[Tutor] Tkinter Menus
Kent Johnson
kent37 at tds.net
Tue Aug 9 13:39:35 CEST 2005
Jorge Louis De Castro wrote:
> Hello,
>
> I'm having this issue with Tkinter. I have a frame with all my buttons,
> checkboxes et al, and all is working fine. I want to add menus and added the
> following to my frame (fCanvas) code:
>
> # create a toplevel menu
> menu = Menu(fCanvas)
> fCanvas.config(menu=menu)
> # create a pulldown menu, and add it to the menu bar
> filemenu = Menu(menu, tearoff=0)
> filemenu.add_command(label="Open", command=self.doBrowse)
> filemenu.add_separator()
> filemenu.add_command(label="Exit", command=self.doQuit)
> menu.add_cascade(label="File", menu=filemenu)
> # create analyze pulldown menu
> analyzemenu = Menu(menu, tearoff=0)
> analyzemenu.add_command(label="Start", command=self.doStart)
> analyzemenu.add_command(label="Reset", command=self.doReset)
> menu.add_cascade(label="Analyze", menu=analyzemenu)
> # create about pulldown menu
> aboutmenu = Menu(menu, tearoff=0)
> menu.add_cascade(label="About", menu=aboutmenu)
>
> Gives me the error:
> Traceback (most recent call last):
> File "C:\Workplace\python\im_logparser\msn_parser_gui.py", line 203, in ?
> myApp = LogAnalyzerGUI()
> File "C:\Workplace\python\im_logparser\msn_parser_gui.py", line 11, in
> __init__
> self.buildUI()
> File "C:\Workplace\python\im_logparser\msn_parser_gui.py", line 35, in
> buildUI
> fCanvas.config(menu=menu)
> File "C:\Python24\Lib\lib-tk\Tkinter.py", line 1139, in configure
> return self._configure('configure', cnf, kw)
> File "C:\Python24\Lib\lib-tk\Tkinter.py", line 1130, in _configure
> self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
> TclError: unknown option "-menu"
>
>
> Unless I replace the menu creation from using my frame to using the root:
>
> root = Tk()
> menu = Menu(root)
> root.config(menu=menu)
>
> But this obviously creates another window which is not what I want at all.
> Any thoughts?
You don't show what fCanvas is...are you creating a Toplevel window? If you just want one window with a menubar you can add your widgets directly to root. Here is a simple example:
from Tkinter import *
def callback(code):
print "called the callback with code", code
root = Tk()
w=Label(root,text="Hello!!")
w.pack()
b=Button(root,text="Bye",command='exit')
b.pack()
# create a menu
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=lambda: callback('New'))
filemenu.add_command(label="Open...", command=lambda: callback('Open'))
filemenu.add_separator()
filemenu.add_command(label="Exit", command='exit')
mainloop()
Kent
More information about the Tutor
mailing list