tkinter menu (newbie question)

Eric Brunel eric.brunel at pragmadev.com
Wed Jun 19 09:33:48 EDT 2002


Stephane Ninin wrote:

>   Hi all,
> 
> I am completely new to kinter and quite new too to Python,
> so ... (sorry if my question has already been asked many times here...)
> 
> 
> here is a simple program I am trying to make
> 
> <--->
> 
> #! /Python22/python
> 
> from Tkinter import *
> 
> class AppWin(Frame):
> 
>     def __init__(self,master=None):
>       Frame.__init__(self,master)
>       self.grid()
>         
>       self.menubar = Menu(self)
> 
>  self.filemenu = Menu(self.menubar)
> 
>  self.filemenu.add_command(label="New")
>  self.filemenu.add_command(label="Open...")
>  self.filemenu.add_command(label="Close")
>  self.filemenu.add_separator()
>  self.filemenu.add_command(label="Quit", command=self.nothing)
> 
> self.editmenu = Menu(self.menubar)
> 
>       self.helpmenu = Menu(self.menubar, name='help')
> 
>       self.helpmenu.add_command(label="About...")
> 
> self.menubar.add_cascade(label="File", menu=self.filemenu)
> self.menubar.add_cascade(label="Edit", menu=self.editmenu)
> self.menubar.add_cascade(label="Help", menu=self.helpmenu)
[snip end of code]

This doesn't suffice. With all that code, you just created a frame that has 
an attribute named menubar with the menu in it, but you didn't do anything 
to actually display the menu (there's no "magical" attributes in Python: if 
you do not do something with an attribute, it is only just that, an 
attribute...).

What you should do is to make MyApp a sub-class of Tk instead of Frame 
(this is more pratical this way), and add at the end of the constructor the 
line:

self.configure(menu=self.menubar)

And that should do the work. Oh: and don't forget to add parameter "self" 
to your "nothing" method, or you'll get a nice traceback when you do 
File->Quit...

HTH
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list