How to enable button if user clicked to the checkbutton

263 views Asked by At

This is my code

def register_user():

        Button(win, text="Sign Up", command=register_file, state=DISABLED).place(x=20, y=290) 
        var = IntVar()
        Checkbutton(win, variable=var,).place(x=15, y=249)

How can I do this

1

There are 1 answers

0
Delrius Euphoria On BEST ANSWER

This is pretty easy, you can use the command option of Checkbutton to get a func to trigger each time you select or unselect the checkbox, like:

def register_user():
        def enable(*args):
            if var.get(): #if the checkbutton is tick
                b['state'] = 'normal' #enable the button
            else: #else
                b['state'] = 'disabled' #disable it

        but = Button(win, text="Sign Up", command=register_file, state=DISABLED)
        but.place(x=20, y=290) 
        var = IntVar()
        cb = Checkbutton(win, variable=var,command=enable) #command option triggers the enable
        cb.place(x=15, y=249)

Why am I saying assigning variables and place() on another line? This is so that the widgets don't becomes None.

The grid, pack and place functions of the Entry object and of all other widgets returns None. In python when you do a().b(), the result of the expression is whatever b() returns, therefore Entry(...).grid(...) will return None.

To understand better take a read from its source, here