You cannot add Checkbutton
or Button
instances to a menu. You must use the add_checkbutton
or add_radiobutton
or add_command
methods. Also, you don't need to create a menubar from a frame and menubuttons -- you can attach a menu directly to a window. Also, you don't need to programatically select or deselect the checkbuttons or radiobuttons, that is taken care of by tkinter.
I've modified your code to illustrate:
from Tkinter import *
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.makeMenu(master)
def makeMenu(self, parent):
menuBar = Menu(parent)
parent.configure(menu=menuBar)
fileMenu = Menu(menuBar)
sourceMenu = Menu(menuBar)
menuBar.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Load")
fileMenu.add_cascade(label="Source", menu=sourceMenu)
self._listi = ["who","what","where","how"]
for l in self._listi:
sourceMenu.add_checkbutton(label=l, command=lambda arg0=l: self.test(arg0))
fileMenu.add_command(label="Save")
fileMenu.add_command(label="Exit", command=self.quit)
def test(self, arg0):
print "you clicked on", arg0
# create app
root = Tk()
myApp = App(root)
# start program
myApp.mainloop()
Note that I also added a line of code to create the root window, and I pass that root window into the constructor for the app.