如果用户单击复选按钮,如何启用按钮

How to enable button if user clicked to the checkbutton

这是我的代码

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)

我该怎么做

这很简单,您可以使用 Checkbuttoncommand 选项来让函数在您每次 select 或取消 select 复选框时触发,喜欢:

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)

为什么我在另一行说分配变量和 place()?这样小部件就不会变成 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.

为了更好地理解,请阅读其来源,here