Cascading menus with Tk

Discussion in 'Python' started by michelle, May 20, 2005.

  1. michelle

    michelle Guest

    Hi all,

    I am new to Tk, or Python GUI programming and I seem to be stuck. I
    have looked about for help with Tk GUIs, but everything seems so terse
    or incomplete?? I have been mostly using the "Introduction to Tkinter"
    by Fredrik Lundh
    (http://www.pythonware.com/library/tkinter/introduction/index.htm)

    What I am trying to do is add cascading menus to a Tk menu widget like:

    File
    New...
    ---> Router
    ---> Firewall
    Open
    ----
    Exit

    This seems simple enough, but I can't get it to work...the two
    "add_cascade" methods (shown below), if used, run an endless loop that
    is difficult to break:

    mainWindow = Tk()
    mainWindow.title("myApp")

    # create a menu
    menubar = Menu(mainWindow)
    mainWindow.config(menu=menubar)

    filemenu = Menu(menubar)
    menubar.add_cascade(label="File", menu=filemenu)
    filemenu.add_command(label="New...")
    filemenu.add_cascade(label="Router")
    filemenu.add_cascade(label="Firewall")
    filemenu.add_command(label="Open...", command = openFileDialog)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command = mainWindow.destroy)

    helpmenu = Menu(menubar)
    menubar.add_cascade(label="Help", menu=helpmenu)
    helpmenu.add_command(label="Online Help")
    helpmenu.add_command(label="Help on the web")
    helpmenu.add_separator()
    helpmenu.add_command(label="About...", command = openAboutBox)

    Any ideas??

    Miki
     
    michelle, May 20, 2005
    #1
    1. Advertising

  2. michelle

    Peter Otten Guest

    michelle wrote:

    > What I am trying to do is add cascading menus to a Tk menu widget like:
    >
    > File
    > New...
    > ---> Router
    > ---> Firewall
    > Open
    > ----
    > Exit


    Just add the submenu with the "Router" and "Firewall" entries to the
    filemenu in the same way you added the submenu with the "New", "Open", and
    "Exit" entries to the menu bar:

    > mainWindow = Tk()
    > mainWindow.title("myApp")
    >
    > # create a menu
    > menubar = Menu(mainWindow)
    > mainWindow.config(menu=menubar)
    >
    > filemenu = Menu(menubar)
    > menubar.add_cascade(label="File", menu=filemenu)


    new_menu = Menu(filemenu)
    new_menu.add_command(label="Router")
    new_menu.add_command(label="Firewall")
    filemenu.add_cascade(label="New...", menu=new_menu)

    > filemenu.add_command(label="Open...", command = openFileDialog)
    > filemenu.add_separator()
    > filemenu.add_command(label="Exit", command = mainWindow.destroy)


    Peter
     
    Peter Otten, May 20, 2005
    #2
    1. Advertising

  3. michelle wrote:
    > Hi all,
    >
    > I am new to Tk, or Python GUI programming and I seem to be stuck. I
    > have looked about for help with Tk GUIs, but everything seems so terse
    > or incomplete?? I have been mostly using the "Introduction to Tkinter"
    > by Fredrik Lundh
    > (http://www.pythonware.com/library/tkinter/introduction/index.htm)
    >
    > What I am trying to do is add cascading menus to a Tk menu widget like:
    >
    > File
    > New...
    > ---> Router
    > ---> Firewall
    > Open
    > ----
    > Exit
    >
    > This seems simple enough, but I can't get it to work...the two
    > "add_cascade" methods (shown below), if used, run an endless loop that
    > is difficult to break:
    >
    > mainWindow = Tk()
    > mainWindow.title("myApp")
    >
    > # create a menu
    > menubar = Menu(mainWindow)
    > mainWindow.config(menu=menubar)
    >
    > filemenu = Menu(menubar)
    > menubar.add_cascade(label="File", menu=filemenu)
    > filemenu.add_command(label="New...")


    I think you want these two options to be in a cascading menu like so:

    newmenu = Menu(filemenu)
    filemenu.add_cascade(label="New...", menu=newmenu)

    newmenu.add_command(label="Router")
    newmenu.add_command(label="Firewall")



    > filemenu.add_cascade(label="Router")
    > filemenu.add_cascade(label="Firewall")
    > filemenu.add_command(label="Open...", command = openFileDialog)
    > filemenu.add_separator()
    > filemenu.add_command(label="Exit", command = mainWindow.destroy)
    >
    > helpmenu = Menu(menubar)
    > menubar.add_cascade(label="Help", menu=helpmenu)
    > helpmenu.add_command(label="Online Help")
    > helpmenu.add_command(label="Help on the web")
    > helpmenu.add_separator()
    > helpmenu.add_command(label="About...", command = openAboutBox)
    >
    > Any ideas??
    >
    > Miki


    Martin
     
    Martin Franklin, May 20, 2005
    #3
  4. michelle

    michelle Guest

    Martin Franklin wrote:
    > michelle wrote:
    >
    >> Hi all,
    >>
    >> I am new to Tk, or Python GUI programming and I seem to be stuck. I
    >> have looked about for help with Tk GUIs, but everything seems so terse
    >> or incomplete?? I have been mostly using the "Introduction to Tkinter"
    >> by Fredrik Lundh
    >> (http://www.pythonware.com/library/tkinter/introduction/index.htm)
    >>
    >> What I am trying to do is add cascading menus to a Tk menu widget like:
    >>
    >> File
    >> New...
    >> ---> Router
    >> ---> Firewall
    >> Open
    >> ----
    >> Exit
    >>
    >> This seems simple enough, but I can't get it to work...the two
    >> "add_cascade" methods (shown below), if used, run an endless loop that
    >> is difficult to break:
    >>
    >> mainWindow = Tk()
    >> mainWindow.title("myApp")
    >>
    >> # create a menu
    >> menubar = Menu(mainWindow)
    >> mainWindow.config(menu=menubar)
    >>
    >> filemenu = Menu(menubar)
    >> menubar.add_cascade(label="File", menu=filemenu)
    >> filemenu.add_command(label="New...")

    >
    >
    > I think you want these two options to be in a cascading menu like so:
    >
    > newmenu = Menu(filemenu)
    > filemenu.add_cascade(label="New...", menu=newmenu)
    >
    > newmenu.add_command(label="Router")
    > newmenu.add_command(label="Firewall")
    >
    >
    >
    >> filemenu.add_cascade(label="Router")
    >> filemenu.add_cascade(label="Firewall")
    >> filemenu.add_command(label="Open...", command = openFileDialog)
    >> filemenu.add_separator()
    >> filemenu.add_command(label="Exit", command = mainWindow.destroy)
    >>
    >> helpmenu = Menu(menubar)
    >> menubar.add_cascade(label="Help", menu=helpmenu)
    >> helpmenu.add_command(label="Online Help")
    >> helpmenu.add_command(label="Help on the web")
    >> helpmenu.add_separator()
    >> helpmenu.add_command(label="About...", command = openAboutBox)
    >>
    >> Any ideas??
    >>
    >> Miki

    >
    >
    > Martin
    >

    Thank you, your code worked perfectly....

    Miki
     
    michelle, May 20, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yavuz Bogazci

    cascading menues with asp.net

    Yavuz Bogazci, Jul 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    383
    Jay Balapa
    Jul 25, 2003
  2. news.west.cox.net

    Select menus above flyout menus? help?

    news.west.cox.net, Dec 8, 2004, in forum: HTML
    Replies:
    8
    Views:
    818
  3. support

    cascading menus

    support, Oct 21, 2008, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    153
    Nathan Sokalski
    Oct 24, 2008
  4. Marci

    Cascading menus in Javascript

    Marci, Jul 18, 2004, in forum: Javascript
    Replies:
    6
    Views:
    127
    Dr John Stockton
    Jul 20, 2004
  5. Cindy Lee

    cascading menus with scrollbars

    Cindy Lee, Aug 21, 2004, in forum: Javascript
    Replies:
    1
    Views:
    118
    Michael Winter
    Aug 21, 2004
Loading...

Share This Page