Tkinter menus

G

Gigs_

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
 
J

James Stroud

Gigs_ said:
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.

James
 
E

Eric Brunel

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
 
G

Gigs_

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+-'])"

btw im tkinter newbie so this question could be stupid

is it alright to use Menu instead Toplevel or Tk
like this?

from Tkinter import *
from tkMessageBox import *

class MenuDemo(Menu):
def __init__(self, master=None):
Menu.__init__(self, master)
self.createWidgets()
self.master.title('Toolbars and Mennus')
self.master.iconname('tkpython')

def createWidgets(self):
self.makeMenuBar()
self.makeToolBar()
L = Label(self.master, text='Menu and Toolbar demo')
L.config(relief=SUNKEN, width=40, height=10, bg='white')
L.pack(expand=YES, fill=BOTH)
 
E

Eric Brunel

is it alright to use Menu instead Toplevel or Tk
like this?

from Tkinter import *
from tkMessageBox import *

class MenuDemo(Menu):
def __init__(self, master=None):
Menu.__init__(self, master)
self.createWidgets()
self.master.title('Toolbars and Mennus')
self.master.iconname('tkpython')

def createWidgets(self):
self.makeMenuBar()
self.makeToolBar()
L = Label(self.master, text='Menu and Toolbar demo')
L.config(relief=SUNKEN, width=40, height=10, bg='white')
L.pack(expand=YES, fill=BOTH)

Feels weird to me. Creating widgets in a window from what is supposed to
its its menu is quite unexpected. I would definitely create a sub-class of
Toplevel or Tk and create the menu in it.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top