Problems with Tkinter and menus

Eric Brunel eric.brunel at pragmadev.com
Thu Dec 13 05:31:06 EST 2001


Hi,

Apparently, the code like you wrote it actually works on Windows, but I
remember that I had a similar problem on Linux, and it was caused by a
problem in the menu hierarchy: have you tried to create menu0 before menu1
and create menu1 as a son of menu0, like in:
menu0 = Tkinter.Menu(root, tearoff=0)
menu1 = Tkinter.Menu(menu0, tearoff=0)
menu1.add_command(label="Foo", command=hello)
# ...
Just try it: it may solve your problem. And doing it this way still works on
Windows.
HTH
 - eric -

"Fredrik Juhlin" <laz at strakt.com> a écrit dans le message news:
mailman.1008148516.7839.python-list at python.org...
> 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