czolbe
czolbe

Reputation: 611

Python tkinter checkbutton issue (unchecking the box calls the attached command)

I have attached a function (comhelms) to my check button. It works normally by calling the function when I check the box. However, unchecking the box calls the function as well. How can I avoid this?

  i = Checkbutton(helmsframe, variable = helmscblist[i], command = comhelms)

Upvotes: 2

Views: 1115

Answers (1)

Josselin
Josselin

Reputation: 2643

You can't prevent the callback from being called when unchecking your checkbutton. But inside the callback function, you can easily use the associated variable to know whether the box was just checked or unchecked:

var = tk.IntVar()

def cb():
    if var.get():
        print("box checked")
    else:
        print("box unchecked")

c = tk.Checkbutton(parent, variable=var, command=cb)

Upvotes: 3

Related Questions