Adding a menu to Tkinter

MRAB python at mrabarnett.plus.com
Mon Oct 26 22:24:59 EDT 2009


Ronn Ross wrote:
> I'm attempting to add a menu bar to my Tkinter app. I can't figure out 
> the correct syntax. Can someone help?  I get this error when I run the app:
> 
> Traceback (most recent call last):
>   File "tkgrid.py", line 26, in <module>
>     app = App(root)
>   File "tkgrid.py", line 10, in __init__
>     menubar = Menu(master).grid(row=0)
>   File 
> "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", 
> line 1859, in grid_configure
>     + self._options(cnf, kw))
> _tkinter.TclError: can't manage ".4300345712": it's a top-level window
> 
> 
> from Tkinter import *
> 
> class App:
> 
>     def __init__(self, master):
>        
>         def hello():
>             print "hello!"
> 
>         menubar = Menu(master).grid(row=0)
>         menubar.add_command(label="Hello!", command=hello)
>         Label(master, text="First").grid(row=1)
>         Label(master, text="Second").grid(row=2)
>    
>         e1 = Entry(master)
>         e2 = Entry(master)
>         e3 = Entry(master)
> 
>         e1.grid(row=0, column=1)
>         e2.grid(row=1, column=1)
>         e2.grid(row=2, column=1)
> 
> 
> root = Tk()
> 
> app = App(root)
> 
> root.mainloop()
> 
This might help:

http://effbot.org/zone/tkinter-menubar.htm




More information about the Python-list mailing list