473,395 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Tkinter menus

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
4 1872
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
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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
2
by: James Ash | last post by:
I'm writing a very simple and small Ptyhon/Tkinter application and I'm having trouble getting the menus to appear correctly. Rather than a name appearing on the menu bar, I see "()" instead. ...
2
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
0
by: Dan Greenblatt | last post by:
I know this can be done for context menus with the 'post' command.... What I'm trying to do, though, is programmatically post non-contextual menus (i.e. menus that exist on a horizontal menu bar...
6
by: Bob Greschke | last post by:
Root.option_add("*?????*font", "Helvetica 12 bold") Want to get rid of the "font =": Widget.add_cascade(label = "File", menu = Fi, font = "Helvetica 12 bold") Does anyone know what ?????...
1
by: MichaelW | last post by:
I've been Python (2.3.4) plus Tkinter happily under MacOX 10.3, having compiled them from scatch. (Tkinter is based on tcl/tk 8.4.1, which were compiled from source via Fink). I then moved my...
0
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
2
by: ishtar2020 | last post by:
Hi everybody I'd appreciate some help on creating a tear off menu with TkInter. I've been reading some documentation but still no luck. Please don't get confused: when I mean "tear off" menu I...
2
by: Andrew Trevorrow | last post by:
Our app uses embedded Python to allow users to run arbitrary scripts. Scripts that import Tkinter run fine on Windows, but on Mac OS X there is a serious problem. After a script does "root = Tk()"...
44
by: bg_ie | last post by:
Hi, I'm in the process of writing some code and noticed a strange problem while doing so. I'm working with PythonWin 210 built for Python 2.5. I noticed the problem for the last py file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.