如何获取 Tkinter Checkbutton 的“状态”?

Tkinter 提供了多种输入小部件,例如条目小部件、文本小部件、列表框、组合框、旋转框、复选框等。复选框用于获取有效性输入,只要用户单击复选框,状态就会激活。对于特定的应用程序,我们可以使用该state()方法检查 tkinter CheckButtons 的状态。它打印 tkinter 检查按钮的实际状态。

示范

#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")
#Define geometry of the window
win.geometry("750x250")
#Create CheckButtons
chk= ttk.Checkbutton(win, text="Python")
chk.pack()
chk.config(state=NORMAL)
print(chk.state())
win.mainloop()
输出结果

运行示例代码将显示一个带有复选按钮的窗口,该复选按钮最初定义为正常状态。

现在,从程序中删除行“ ”以在控制台上打印复选框的状态。win.mainloop()

('alternate',)