[Tutor] Tkinter Menus

Jorge Louis de Castro jobauk at hotmail.com
Tue Aug 9 16:29:00 CEST 2005


Ah, sorry. Here is the definition of fCanvas:

class LogAnalyzerGUI(Frame):
  def __init__(self, parent=0):
    Frame.__init__(self, parent)
    self.type = 2
    self.terms = 0
    self.master.title('XML Analyzer')
    self.buildUI()

  def buildUI(self):
    fCanvas = Frame(self)
    Label(fCanvas, text="Filename: ").grid(row=0, sticky=E)
    ...

> > 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:

Yes, I know that. I tried that and saw, as expected, that it popped a window 
outside my fCanvas frame, whereas I want to add the menu to the fCanvas 
frame the same way I added the labels ,buttons, etc. The interpreter 
complains that it doesn't know the 'menu' configuration option from the 
config method. This works as expected if the root is used like you say

>menu = Menu(root)
>root.config(menu=menu)

But doesnt if I want to add it to fCanvas:

menu = Menu(fCanvas)
fCanvas.config(menu=menu)

Chrs
j.




More information about the Tutor mailing list