[Tutor] Menu woes

Gregor Lingl glingl@aon.at
Thu Nov 14 12:42:26 2002


Hi Robert!

Although I'm not a Tkinter-expert, I'll try an answer - if there's 
something wrong
in it, it will certainly be corrected by other readers of the list:

There are apperently two problems in your code:

1. Frame doesn't have an option menu - in contrast to the Toplevel-Widget
or Tk. So use this instead.

If you make this change, the program will run without error-message but
unfortunately also without displaying the menu.

2. To change this, you have to add then filemenu to the menubar by using
the add_cascade - method

So the following code - hopefully - does what you want:

class Application(Tk):
      def say_hi(self):
            print "hi there, everyone!"

      def createWidgets(self):
            self.QUIT = Button(self)
            self.QUIT["text"] = "QUIT"
            self.QUIT["fg"]   = "red"
            self.QUIT["command"] = self.quit
            self.QUIT.pack({"side": "left"})
            self.hi_there = Button(self)
            self.hi_there["text"] = "Hello",
            self.hi_there["command"] = self.say_hi
            self.hi_there.pack({"side": "left"})
            self.menubar = Menu(self)
            self.filemenu = Menu(self.menubar, tearoff=0)
            self.filemenu.add_command(label="Open", command=self.say_hi)
            self.menubar.add_cascade(label="Actions", menu=self.filemenu)
            self.config(menu=self.menubar)

      def __init__(self, master=None):
            Tk.__init__(self, master)
            # self.pack()
            self.createWidgets()

app = Application()
app.mainloop()

HTH, Gregor


Robert M. Anderson schrieb:

I'm having trouble with a short program I copied and modified from somewhere. The code is as follows:

### short program starts here

from Tkinter import *

class Application(Frame):
      def say_hi(self):
            print "hi there, everyone!"

      def createWidgets(self):
            self.QUIT = Button(self)
            self.QUIT["text"] = "QUIT"
            self.QUIT["fg"]   = "red"
            self.QUIT["command"] = self.quit
            self.QUIT.pack({"side": "left"})
            self.hi_there = Button(self)
            self.hi_there["text"] = "Hello",
            self.hi_there["command"] = self.say_hi
            self.hi_there.pack({"side": "left"})
            self.menubar = Menu(self)
            self.filemenu = Menu(self.menubar, tearoff=0)
            self.filemenu.add_command(label="Open", command=self.say_hi)
            self.config(menu=self.menubar)

      def __init__(self, master=None):
            Frame.__init__(self, master)
            self.pack()
            self.createWidgets()

app = Application()
app.mainloop()

### short program ends here

It works fine if I get rid of the menu code, which is what I added based on something from Fredrik Lundh's Tkinter introduction site. The error I get from Python is for the "self.config" command, saying:

TclError: unknown option "-menu"

Please help if you can!

Regards,

Robert M. Anderson