So maybe this is a pretty basic question, then again I am new to python so please bear with me...

I have some code, and I have a tkInter checkbox. I figured out how to get the on/off value of the check box but I dont really understand why I need to do it the way I do.

def __init__(self, master):
        self.var = IntVar()
        c = Checkbutton(master, text="TEST_BUTTON",
                        variable=self.var, command=self.testFunc)
        c.pack()

    def testFunc(self, event):
        print self.var.get()

My question is; self.var.get() gets me the 0/1 value of var which is what I want so I can eventually use the value as a flag to preform some function. It took me a while to figure this out though.

Why doesnt print self.var give me the 0/1 value of the check box?

Recommended Answers

All 3 Replies

For flaging tk has tags have you considered those?

The self.var is an object, not an integer. This object contains some other methods that help it stay in sync with your checkbox. When you click the checkbox, Tcl generates an event that is handled and the self.var.set() method is called at some point. But if self.var would simply be an integer, that would not be possible. self.var.get() returns the value which is an integer.

When you use
self.var = IntVar()
you are really creating an instance of a class
this class has get() and set() methods

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.