[py3] Tkinter menu checkbutton not working

Terry Reedy tjreedy at udel.edu
Wed Jun 9 12:55:28 EDT 2010


On 6/9/2010 12:26 PM, Dodo wrote:
> Hello,
>
> I trying to make this piece of code work (this is python3)
>
> from tkinter import *
> from tkinter.ttk import *
>
> class Window:
> def __init__(self):
> self.root = Tk()
>
> self.menu = Menu(self.root)
> self.root['menu'] = self.menu
>
> self.submenu = Menu(self.menu)
> self.ck = 0
> self.submenu.add_checkbutton(label="My checkbutton", variable=self.ck,
> command=self.displayCK)
> self.menu.add_cascade(label="sub", menu=self.submenu )
>
> def displayCK(self):
> print( self.ck )
>
> app = Window()
> app.root.mainloop()
>
> The self.ck will always be 0... why?

You never change it ;-)
Passing the *value* 0 to the widget has no effect on the binding of 
self.ck. You need to pass a container whose contents the widget can 
modify == specifically an IntVar. See 24.1.6.4. Coupling Widget Variables.

Terry Jan Reedy





More information about the Python-list mailing list