Problems with Tkinter and menus

Fredrik Juhlin laz at strakt.com
Wed Dec 12 04:14:04 EST 2001


Hello,

I'm trying to make cascading popup menus work in Tkinter. Unfortunately, I'm 
having little success.
The problem is that the commands under the second-level menus won't run when
clicked on. Actually, it seems like they are not responding to the mouse at
all, since hovering on a menu-item doesn't "mark" it.

I've submitted test code to show the problem. 

Any help on this would be greatly appreciated!

/FJ

# Code to show problem with cascading menus

import Tkinter

def hello():
    print "hello!"

root = Tkinter.Tk()

menu1 = Tkinter.Menu(root, tearoff=0)
menu1.add_command(label="Foo", command=hello)
menu1.add_command(label="Bar", command=hello)

menu0 = Tkinter.Menu(root, tearoff=0)
menu0.add_command(label="Command 1", command=hello) #This works
menu0.add_cascade(label="Menu 1", menu=menu1) #Commands used in this menu
                                              #won't work

frame = Tkinter.Frame(root, width=200, height=200)
frame.pack()

def showMenu(event):
    menu0.post(event.x_root, event.y_root)

frame.bind("<Button-3>", showMenu)

Tkinter.mainloop()




More information about the Python-list mailing list