FAQ
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 0, height 0)
frame.pack()

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

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

Tkinter.mainloop()

Search Discussions

  • Greg Krohn at Dec 12, 2001 at 10:41 am
    "Fredrik Juhlin" <laz at strakt.com> wrote in 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 sniped]

    Works fine for me: Win2K/Python 2.1.1/Tcl 8.3.
    It prints 'hello!' for 'Command1', 'Foo', and
    'Bar'. Have you tried restarting Python? Maybe
    something got reassigned somehow.


    greg
  • Fredrik Juhlin at Dec 12, 2001 at 11:20 am

    "Greg Krohn" wrote:
    Works fine for me: Win2K/Python 2.1.1/Tcl 8.3.
    It prints 'hello!' for 'Command1', 'Foo', and
    'Bar'. Have you tried restarting Python? Maybe
    something got reassigned somehow.
    The problem persists for me, unfortunately.
    I'm using Linux 2.4.5/XFree86 4.1.0/Python 2.1/Tcl 8.3. I've tried KDE 2.1.1
    and Fvwm 2.2.5 with the same result.

    //FJ
  • Greg Krohn at Dec 13, 2001 at 1:35 am
    "Fredrik Juhlin" <laz at strakt.com> wrote in message
    news:mailman.1008156077.1151.python-list at python.org...
    "Greg Krohn" wrote:
    Works fine for me: Win2K/Python 2.1.1/Tcl 8.3.
    It prints 'hello!' for 'Command1', 'Foo', and
    'Bar'. Have you tried restarting Python? Maybe
    something got reassigned somehow.
    The problem persists for me, unfortunately.
    I'm using Linux 2.4.5/XFree86 4.1.0/Python 2.1/Tcl 8.3. I've tried KDE 2.1.1
    and Fvwm 2.2.5 with the same result.

    //FJ
    Yeah, I just tried it under linux (2.9.?something?) and it didn't work for
    me either.
    That's Python 2.1, also, so it must be a linux thing. I didn't see a bug
    report. I
    know about as much about linux as I know about the Hubble Space Telescope,
    so I'm just
    going to throw my hands up in the air.

    greg
  • Eric Brunel at Dec 13, 2001 at 10:31 am
    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 0, height 0)
    frame.pack()

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

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

    Tkinter.mainloop()
  • Fredrik Juhlin at Dec 13, 2001 at 11:21 am

    On Thu, Dec 13, 2001 at 11:31:06AM +0100, Eric Brunel wrote:
    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.
    This worked fine! And like so many other thing, painfully obvious once
    someone points it out :)

    Thanks a lot!

    //FJ
  • Bill Melcher at Dec 13, 2001 at 4:13 pm
    I am running Python 2.1.1 under Win98 and tried:

    import Tkinter

    def hello():
    print "hello!"

    root = Tkinter.Tk()

    menu0 = Tkinter.Menu(root, tearoff=0)
    menu1 = Tkinter.Menu(root, tearoff=0)

    menu1.add_command(label="Foo", command=hello)
    menu1.add_command(label="Bar", command=hello)

    menu0.add_command(label="Command 1", command=hello)
    menu0.add_cascade(label="Menu 1", menu=menu1)

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

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

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

    Tkinter.mainloop()

    No hello, no menus and no errors reported. The origional version posted by
    Fredrik Juhlin showed the same behavior.

    Any ideas?

    --
    Cheers, Bill
    TANSTAAFL!
  • Fredrik Juhlin at Dec 13, 2001 at 4:50 pm
    On Thu, Dec 13, 2001 at 04:13:11PM +0000, Bill Melcher wrote:
    [snipped beginning of code]
    What if you try:
    menu0 = Tkinter.Menu(root, tearoff=0)
    menu1 = Tkinter.Menu(menu0, tearoff=0)
    rather than:
    menu0 = Tkinter.Menu(root, tearoff=0)
    menu1 = Tkinter.Menu(root, tearoff=0)
    [snipped remaining code]
    No hello, no menus and no errors reported. The origional version posted by
    Fredrik Juhlin showed the same behavior.
    Of course, if absolutely nothing happened and you didn't even get a menu
    when you right-clicked on the window, then something seems even more broken
    for you.

    //FJ
  • Bill Melcher at Dec 13, 2001 at 8:09 pm

    Of course, if absolutely nothing happened and you didn't even get a menu
    when you right-clicked on the window, then something seems even more broken
    for you.
    Sorry, not quite that broken. There is a window with Tk in the top red
    border, the usual minimize, full screen and close buttons and right click
    has the usual entries. What is missing is text in the window and the
    cascaded menus specified in the code.

    In addition, with help from the group, I got PySol 4.72 (sadly, not 4.80)
    running here so I think Python is probably not broken.

    Since both versions are said to work under Win2K and the corrected version
    works with Linux, I guess what I need now is a Win98 guy.

    --
    Cheers, Bill
    TANSTAAFL!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 12, '01 at 9:14a
activeDec 13, '01 at 8:09p
posts9
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase