Tkinter checkbox executing command only once

Viewed 152
x = tk.IntVar()
def test():
   print(x.get())
checkBox = tk.Checkbutton(text="test",command=test(),variable=x)
checkBox.grid(row=12,column=2)

b=tk.Button(text="test",command=test)
b.grid(row=12,column=0

this is some code I put in my program for testing, when I check the checkbox the value does change (I can confirm it with the button I made to test) but the command does not get executed.

1 Answers

Remove () in command

command= test

This works fine:

import tkinter as tk

win = tk.Tk()
x = tk.IntVar()


def test():
    print(x.get())


checkBox = tk.Checkbutton(text="test", command=test, variable=x)
checkBox.grid(row=12, column=2)
win.mainloop()
Related