Re: Get menu index from label
Bryan Oakley <[email protected]> Fri, 14 Apr 2023 17:35:59 -0500
| Newsgroups | gmane.comp.python.tkinter |
|---|---|
| Message-ID | <CAKypQnxOWnb3zhiDE8CfMPgxqpeaypH1TefZJC0UPbS2k3pMHA@mail.gmail.com> |
--===============1933255496081291954==
Content-Type: multipart/alternative; boundary="000000000000c6f35705f95377fe"
--000000000000c6f35705f95377fe
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
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=3D"disabled")
If you want to get the numerical index of an item, you can use the index
method.
index =3D file_menu.index("Save As")
On Fri, Apr 14, 2023 at 4:54=E2=80=AFPM Alan Gauld via Tkinter-discuss <
[email protected]> 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
> [email protected]
> https://mail.python.org/mailman/listinfo/tkinter-discuss
>
--000000000000c6f35705f95377fe
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">You can use the label string as the index. Or more accurat=
ely, a pattern that matches an element. Once tkinter finds an item that mat=
ches the pattern, that item will be used. For example, "Save*" mi=
ght match both "Save" and "Save As...", but whichever o=
ne comes first in the menu is the one that will be used.<div><div><br></div=
><blockquote style=3D"margin:0 0 0 40px;border:none;padding:0px"><div><font=
face=3D"monospace">file_menu.entryconfig("Save*", state=3D"=
disabled")</font></div></blockquote><div><br></div><div>If you want to=
get the numerical index of an item, you can use the <font face=3D"monospac=
e">index</font> method.</div><div><br></div></div><blockquote style=3D"marg=
in:0 0 0 40px;border:none;padding:0px"><div><font face=3D"monospace">index =
=3D file_menu.index("Save As")</font></div></blockquote><div><br>=
</div><div><br></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" =
class=3D"gmail_attr">On Fri, Apr 14, 2023 at 4:54=E2=80=AFPM Alan Gauld via=
Tkinter-discuss <<a href=3D"mailto:[email protected]">tkinter-=
[email protected]</a>> wrote:<br></div><blockquote class=3D"gmail_quote=
" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);=
padding-left:1ex">I'm trying to extract a menu entry's index given =
the<br>
entry label.<br>
ie for the typical File... menu, with entries of:<br>
New, Open, Save, Save As, etc...<br>
<br>
How do I get the index of the entry labelled<br>
"Save As", given I have a reference to the<br>
containing menu object?<br>
<br>
Once I have an index I can do various things to the<br>
entry but there seems to be no way to access the entry<br>
without an index and no way to get the index (other<br>
than counting manually, which is no good where the<br>
menu content changes dynamically) based on the label.<br>
<br>
The top level requirement is to toggle the label<br>
between two values when certain state changes occur.<br>
(I could use a checkbox menuitem but those are not<br>
common UI elements and might confuse some users.)<br>
<br>
[ Another solution would be to get the list of items<br>
in the menu, but I can't see how to do that either<br>
(which seems odd!) With that list I could loop to<br>
find the item/index. ]<br>
<br>
-- <br>
Alan G<br>
Author of the Learn to Program web site<br>
<a href=3D"http://www.alan-g.me.uk/" rel=3D"noreferrer" target=3D"_blank">h=
ttp://www.alan-g.me.uk/</a><br>
<a href=3D"http://www.amazon.com/author/alan_gauld" rel=3D"noreferrer" targ=
et=3D"_blank">http://www.amazon.com/author/alan_gauld</a><br>
Follow my photo-blog on Flickr at:<br>
<a href=3D"http://www.flickr.com/photos/alangauldphotos" rel=3D"noreferrer"=
target=3D"_blank">http://www.flickr.com/photos/alangauldphotos</a><br>
<br>
<br>
_______________________________________________<br>
Tkinter-discuss mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Tkinter-dis=
[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/tkinter-discuss" rel=3D=
"noreferrer" target=3D"_blank">https://mail.python.org/mailman/listinfo/tki=
nter-discuss</a><br>
</blockquote></div>
--000000000000c6f35705f95377fe--
--===============1933255496081291954==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Tkinter-discuss mailing list
[email protected]
https://mail.python.org/mailman/listinfo/tkinter-discuss
--===============1933255496081291954==--