Lazy cascading menu in Tkinter with postcommand

Humpty Dumpty oliver.schoenborn at utoronto.ca
Fri Jan 16 22:46:25 CET 2004


Hi folks, here's a challenge: I have a dynamically created cascading menu in
Tkinter that can be quite large because it is created from a file. I tried
using lazy creation so only the menu item that is actually selected by the
user gets children menu items created under it, but that fails.

I did this by use of postcommand callback, in which I dynamically add the
children menu items to the parent. However, a print statement in the
postcommand callback shows that:
1) *ALL* cascading menu items in a hierarchy get the postcommand event,
every time the user selects the top menu item
2) all children that have just been added to a cascading menu item in the
postcommand callback will hear that postcommand event

So in the end, the first time the user clicks on the top menu item, the
whole menu hierarchy gets created (though only the top level is displayed of
course).

Anyone know how to get around this? I'm considering bypassing the built-in
cascading menu and creating my own via popup menus and filtering for "on
entry" of the buttons, but there should be a simpler method.

Thanks in advance for any help,
Oliver





More information about the Python-list mailing list