[Tkinter-discuss] Get menu index from label
Bryan Oakley
bryan.oakley at gmail.com
Fri Apr 14 18:35:59 EDT 2023
You can use the label string as the index. Or more accurately, a pattern
that matches an element. Once tkinter finds an item that matches the
pattern, that item will be used. For example, "Save*" might match both
"Save" and "Save As...", but whichever one comes first in the menu is the
one that will be used.
file_menu.entryconfig("Save*", state="disabled")
If you want to get the numerical index of an item, you can use the index
method.
index = file_menu.index("Save As")
On Fri, Apr 14, 2023 at 4:54 PM Alan Gauld via Tkinter-discuss <
tkinter-discuss at python.org> wrote:
> I'm trying to extract a menu entry's index given the
> entry label.
> ie for the typical File... menu, with entries of:
> New, Open, Save, Save As, etc...
>
> How do I get the index of the entry labelled
> "Save As", given I have a reference to the
> containing menu object?
>
> Once I have an index I can do various things to the
> entry but there seems to be no way to access the entry
> without an index and no way to get the index (other
> than counting manually, which is no good where the
> menu content changes dynamically) based on the label.
>
> The top level requirement is to toggle the label
> between two values when certain state changes occur.
> (I could use a checkbox menuitem but those are not
> common UI elements and might confuse some users.)
>
> [ Another solution would be to get the list of items
> in the menu, but I can't see how to do that either
> (which seems odd!) With that list I could loop to
> find the item/index. ]
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> https://mail.python.org/mailman/listinfo/tkinter-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/tkinter-discuss/attachments/20230414/4c472a31/attachment.html>
More information about the Tkinter-discuss
mailing list