So, I was learning about checkbuttons
in Tkinter
and I am stuck with this wierd issue. This is the code that I am using
root = Tk()
def showValue():
print(val.get())
Label(text=str(val.get())).pack()
val = StringVar()
Checkbutton(root, text="Check me!", variable=val, onvalue="on", offvalue="off").pack()
Button(text="Click",command=showValue).pack()
root.mainloop()
Firstly, the box is checked by default, which I think is because the offvalue
is set to something i.e not False (i.e not 0 or an empty string), please correct me if I am wrong. But, the weird thing that I notice here is that when I hit the button, which should ideally show me a Label with text="on"
, but actually it doesn't until I deselect the box manually, after which the button behaves normally. I know I couldn't explain whats happening here properly, but if you try it out yourself you would see it.
Here's an image of the GUI.
Now, the two blank lines that you see after the button is actually because I clicked the button twice before doing anything with the checkbox, and once I deselected the checkbox and then clicked the button, it displayed off
. Can someone please what's actually happening here? Thanks in advance!