By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,635 Members | 1,699 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,635 IT Pros & Developers. It's quick & easy.

Tkinter menus

P: n/a
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
Mar 1 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

James
Mar 1 '07 #2

P: n/a
On Thu, 01 Mar 2007 22:35:40 +0100, James Stroud <js*****@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+-'])"
Mar 2 '07 #3

P: n/a
>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)
Mar 2 '07 #4

P: n/a
On Fri, 02 Mar 2007 13:41:12 +0100, Gigs_ <gi**@hi.t-com.hrwrote:
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
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Mar 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.