TKinter中的禁用/启用按钮
我正在尝试使按钮像开关一样,因此,如果单击禁用按钮,它将禁用“按钮”(有效)。如果我再按一次,它将再次启用它。
我尝试过诸如if,else之类的事情,但没有成功。这是一个例子:
from tkinter import *
fenster = Tk()
fenster.title("Window")
def switch():
b1["state"] = DISABLED
#--Buttons
b1=Button(fenster, text="Button")
b1.config(height = 5, width = 7)
b1.grid(row=0, column=0)
b2 = Button(text="disable", command=switch)
b2.grid(row=0,column=1)
fenster.mainloop()
-
Tkinter
Button
具有三种状态:active, normal, disabled
。您将
state
选项设置disabled
为灰色按钮,使其无响应。active
鼠标悬停在其上时具有该值,默认值为normal
。使用此功能,您可以检查按钮的状态并采取所需的措施。这是工作代码。
from tkinter import * fenster = Tk() fenster.title("Window") def switch(): if b1["state"] == "normal": b1["state"] = "disabled" b2["text"] = "enable" else: b1["state"] = "normal" b2["text"] = "disable" #--Buttons b1 = Button(fenster, text="Button", height=5, width=7) b1.grid(row=0, column=0) b2 = Button(text="disable", command=switch) b2.grid(row=0, column=1) fenster.mainloop()