Tkinter menus
Eric Brunel
eric_brunel at despammed.com
Fri Mar 2 03:39:50 EST 2007
On Thu, 01 Mar 2007 22:35:40 +0100, James Stroud <jstroud at mbi.ucla.edu>
wrote:
> Gigs_ wrote:
>> class MenuDemo(Frame):
>> def __init__(self, parent=None):
>> Frame.__init__(self, parent)
>> self.pack(expand=YES, fill=BOTH)
>> self.createWidgets()
>> def createWidgets(self):
>> self.makeMenuBar()
>> self.makeToolBar()
>> L = Label(self, text='Menu and Toolbar demo')
>> L.config(relief=SUNKEN, width=40, height=10, bg='white')
>> L.pack(expand=YES, fill=BOTH)
>> def makeMenuBar(self):
>> self.menubar = Menu(self.master) #here
>> self.master.config(menu=self.menubar) #here
>> self.fileMenu()
>> self.editMenu()
>> self.imageMenu()
>> why i need to use self.master?
>> why i cant just use self?
>> thx
>
> master is a reference to a Tk() or Toplevel(). Frames do not contain
> menus, but the windows that contain them do.
This is the main reason why I always rant about examples of Tkinter
programming creating windows by sub-classing Frame: frames are not
windows. If you want to create a window, sub-class Toplevel (or Tk), not
Frame. A frame is a general-purpose container. It can be sub-classed to
create new "mega-widgets" that can be used in any context. This is
obviously not the case in the code above, as the parent passed to any
instance of MenuDemo *must* be a Toplevel or Tk. So just write:
class MenuDemo(Toplevel):
def __init__(self):
Toplevel.__init__(self)
...
def makeMenuBar(self):
self.menubar = Menu(self)
self.config(menu=self.menubar)
...
and your life will be easier ;-)
HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
More information about the Python-list
mailing list