user13843229
user13843229

Reputation:

Tkinter CheckButton state not changing

I am making a login page with tkinter..

So when the user clicks on the CheckButton, I want the password to be displayed and when it is unchecked, I want it to be hidden... (star marked)

I am not able to change the state of the CheckButton...

Have viewed several answers but it did not solve my issue

My sample code below:

from tkinter import *

win = Tk()
var = IntVar()
chk = Checkbutton(win, text='See password', variable=var)
chk.grid(row=4, column=1, pady=5)

if var.get():
    print("Checked")
else:
    print("Not checked..")

win.mainloop()

When I run the code, the default is unchecked. So after I check, it does not print Checked.

Upvotes: 0

Views: 526

Answers (1)

CC7052
CC7052

Reputation: 577

You can bind your IntVar() to a function, if your IntVar() changed, youor function will execute:

from tkinter import *


def callback(a, b, c):
    if var.get():
        print("Checked")
    else:
        print("Not checked..")


win = Tk()
var = IntVar()
var.trace('w', callback)
chk = Checkbutton(win, text='See password', variable=var)
chk.grid(row=4, column=1, pady=5)

win.mainloop()

Upvotes: 0

Related Questions