如何在 Tkinter 中禁用(变灰)复选按钮?

Tkinter 提供了多种输入小部件,例如条目小部件、文本小部件、列表框、组合框、旋转框、复选框等。复选框用于获取有效性输入,只要用户单击复选框,状态就会激活。对于特定的应用程序,我们可以通过使用state属性来启用和禁用 CheckButtons 的状态。

示范

#Import the required library
from tkinter import*
from tkinter import ttk
#Create an instance of tkinter frame
win= Tk()
#Set the geometry
win.geometry("750x250")
#Create CheckButtons
chk= ttk.Checkbutton(win, text="Python")
chk.pack()
chk.config(state=DISABLED)
win.mainloop()
输出结果

运行示例代码将显示一个带有最初禁用的复选按钮的窗口。

我们可以通过将state 属性的值更改为 NORMAL 或 DISABLED 来更改复选按钮的状态。