Disable / Enable Button in TKinter

后端 未结 2 1276
没有蜡笔的小新
没有蜡笔的小新 2020-12-17 17:30

I\'m trying to make a button like a switch, so if I click the disable button it will disable the \"Button\" (that works). And if I press it again, it will enable it again.

2条回答
  •  [愿得一人]
    2020-12-17 18:13

    The problem is in your switch function.

    def switch():
        b1["state"] = DISABLED
    

    When you click the button, switch is being called each time. For a toggle behaviour, you need to tell it to switch back to the NORMAL state.

    def switch():
        if b1["state"] == NORMAL:
            b1["state"] = DISABLED
        else:
            b1["state"] = NORMAL
    

提交回复
热议问题