Cascading menus with Tk

M

michelle

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
 
P

Peter Otten

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

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

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
 
M

Martin Franklin

michelle said:
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
 
M

michelle

Martin said:
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")





Martin
Thank you, your code worked perfectly....

Miki
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top