From alan.gauld at yahoo.co.uk Fri Apr 14 14:11:21 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 14 Apr 2023 19:11:21 +0100 Subject: [Tkinter-discuss] Get menu index from label Message-ID: 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 From alan.gauld at yahoo.co.uk Fri Apr 14 14:18:10 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 14 Apr 2023 19:18:10 +0100 Subject: [Tkinter-discuss] Running from a cron job? In-Reply-To: <89EEFE4E-1380-4929-B6C6-15A680297B33@greschke.com> References: <89EEFE4E-1380-4929-B6C6-15A680297B33@greschke.com> Message-ID: On 15/02/2023 20:58, Bob Greschke wrote: > Hi! Hi, sorry for a late response. > I'm writing a tkinter utility, more than a program, Why use Tkinter if there is no GUI? > plots the data from the file(s) that do not have a > corresponding image file, does an ImageGrab.grab() of the Canvas Could you not create the image directly using PIL/Pillow, say? I've no experience of plotting data directly to an image file but it sounds like it should be viable and avoids Tkinter. Just a thought. Again, no personal experience but matplotlib or gnuplot may also be able to work directly with files. It seems likely. -- 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 From bryan.oakley at gmail.com Fri Apr 14 18:35:59 2023 From: bryan.oakley at gmail.com (Bryan Oakley) Date: Fri, 14 Apr 2023 17:35:59 -0500 Subject: [Tkinter-discuss] Get menu index from label In-Reply-To: References: Message-ID: 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: From alan.gauld at yahoo.co.uk Fri Apr 14 19:35:27 2023 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 15 Apr 2023 00:35:27 +0100 Subject: [Tkinter-discuss] Get menu index from label In-Reply-To: References: Message-ID: On 14/04/2023 23:35, Bryan Oakley wrote: > 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") I knew there must be a simple way, it seemed such an obvious thing to want to do. But I couldn't find anything about using the label as an index in any of my Tk/Tkinter books (and I have 6 covering Tkinter and 2 covering Tcl/Tk!) I got numerical indices and symbolic ones like "first" and "last" but no mention of using the label string (and especially not a partial string pattern). > If you want to get the numerical index of an item, you can use the index > method. > > index = file_menu.index("Save As") And this makes much more sense now, I couldn't fathom out what menu.index(index) meant! Thanks so much. -- 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