Tkinter menuitem question

travisnixon at my-deja.com travisnixon at my-deja.com
Mon Oct 23 02:48:56 EDT 2000


Quick question about the items in a Tkinter Menu():

Given the root menu, I want to iterate through all the items in the
menu (including cascaded submenus) and change their 'command' entries.

Ok, let me rephrase.  What I ACTUALLY want to do is to be able to reload
() a file that is referenced by a menuitem's command, and have the
menuitem call the new function instead of the old one.

The way I'm trying right now to do this is by doing the following:
Before reloading, search the entire menu structure, and see if the
referenced command is in the module that's about to be reloaded.  If
so, add it to a list of commands.  Reload the module.  Go back through
the list (which contains the name of the function the command
referenced) and relink all the commands to the new 'version' of the
same function they called before.

Hope somebody can make sense of that, cause I'm not sure I can. :)

Anyway, if somebody could tell me how to iterate through a menu's
commands, that would be good.  The problem I'm having is this:

menu.type(0) will give me the type of item 0, so I know if it's a
command or not.  I could then get the configuration, and do everything
I need to be able to do.  I could also do this same thing for menu.type
(1), menu.type(2), etc etc.  HOWEVER, it seems that menu.type(1348)
returns the same type as menu.type(1) given a menu with 2 items.  How
do I know how many there are?  One thing I thought of is to check to
see if menu.index(i) is equal to i, but I don't know if this is really
the correct way to approach it.  (for example, I have no idea if there
might be a 'missing' index somewhere, so that menu.index(1) = 0, but
menu.index(2) = 2 and is a valid item.

Egads.

Anybody have good news for me? :)  Or maybe an easier solution to the
reload problem I'm having? :)  And yes, I really do need to be able to
reload and have menu commands reference the new functions.  I can't
layer the calls, because the commands themselves come out of an option
file that looks something like this:

[rootmenu]
Items = File::Submenu::filemenu

[filemenu]
Items = Exit::pymisc::Exit

In this example, pymain is the name of a python module that is imported
when the menu is parsed, and the Exit menuitem has its 'command' set to
Exit in pymain.py.  The importing itself was quite a pain in the butt
(mostly because I'm still fairly new at this python thing, and didn't
even think about using the exec command till I had tried everything
under the sun using __import__), but I got it working.  Now I can't
figure out the reload part, though.  :)

Any input welcome, and thanks much,
Travis Nixon


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list