pythoneventstkinter

Tkinter Checkbutton and event callback function


Here is a little code example I found on the Effbot website which is close to what I want to do in one of my programs:

from Tkinter import *

fen =Tk()

class test_Tk_class:
    def __init__(self):
        self.var = IntVar()
        c = Checkbutton(
            fen, text="Enable Tab",
            variable=self.var,
            command=self.cb)
        c.pack()

    def cb(self,event):
        print "variable is", self.var.get()

    a = test_Tk_class()
    fen.mainloop()

However this code is not working. The callback function cb does not work because it takes 2 arguments and none are given. How do you specify the event argument?


Solution

  • This code does not need event at all in this case. I got it working by just removing it altogether:

    def cb(self):
        print "variable is", self.var.get()
    

    The only time you would structure your code that way is if you are binding functions to key presses or mouse clicks. For checking/unchecking a checkbutton however, it is not needed.

    I don't know what the person who coded this on Effbot was trying to do, but I don't think he did it right. Maybe he made a typo or had something else in mind.