Touble with Tkinter menus (code included)
Eric Brunel
eric.brunel at pragmadev.N0SP4M.com
Mon Oct 20 04:41:43 EDT 2003
James Ash wrote:
> 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. Clicking on these "()" does
> nothing (other than changing the appearance of them to indicated they've
> been pressed).
>
> I'm using Python 2.2.3 on Win2K, using a release downloaded from one
> of the Cygwin mirrors.
>
> This is most likely a simple mistake on my part, but I can't find it. I'm new
> to Python and Tkinter both.
>
> Any help appreciated!
> Jim
>
>
> #! /usr/bin/env python
>
> # $Id$
> #
> # File: timecard.py
>
> import string
>
>
> from Tkinter import *
>
>
>
> class App:
>
> def callback(self):
> print "called the callback!"
>
> def __init__(self, master):
> frame=Frame(master)
> master.title("Timecard, Implemented in Cygwin supplied Python!")
> master.maxsize(1000, 400)
> frame.pack()
>
> self.b = Button(frame, text="Clock In", width=8, command=self.callback)
> self.b.pack(side=LEFT, padx=2, pady=2)
>
> self.b = Button(frame, text="Clock Out", width=8, command=self.callback)
> self.b.pack(side=LEFT, padx=2, pady=2)
>
> self.b = Button(frame, text="Report", width=8, command=self.callback)
> self.b.pack(side=LEFT, padx=2, pady=2)
>
> self.menubar = Menu(master)
>
> self.filemenu=Menu(self.menubar)
> self.filemenu.add_command(master, label="Exit", command=self.callback)
> self.menubar.add_cascade(master, label="File", menu=self.filemenu)
No need for master in the two previous calls: doing just
self.filemenu.add_command(label="Exit", command=self.callback)
self.menubar.add_cascade(label="File", menu=self.filemenu)
solves the problem.
Please refer to http://www.pythonware.com/library/tkinter/introduction/index.htm
for further details.
>
> master.config(menu=self.menubar)
>
> root = Tk()
>
> app = App(root)
> root.mainloop()
>
HTH
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
More information about the Python-list
mailing list