Cascading menus with Tk

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Fri May 20 09:10:33 EDT 2005


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




More information about the Python-list mailing list