Python 获取 Checkbutton 状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4236910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:53:03  来源:igfitidea点击:

Getting Checkbutton state

pythontkintertkinter.checkbutton

提问by rectangletangle

How do I get the 'state'of a Tkinter Checkbutton? By 'state'I mean get whether or not it has a check mark in it or not.

我如何获得'state'a 的Tkinter Checkbutton?通过'state'我的意思是得到它是否有一个复选标记与否。

采纳答案by aaronasterling

When you're creating it, it takes a variablekeyword argument. Pass it an IntVarfrom Tkinter. Checking or unchecking the box will set that value contained by varto the corresponding boolean state. This can be accessed as var.get():

创建它时,它需要一个variable关键字参数。它传递的IntVar距离Tkinter。选中或取消选中该框会将包含的值设置var为相应的布尔状态。这可以访问为var.get()

checked => var.get()

检查 => var.get()

not checked => not var.get()

没有检查=> not var.get()

>>> root = Tkinter.Tk()
>>> var = Tkinter.IntVar()
>>> chk = Tkinter.Checkbutton(root, text='foo', variable=var)
>>> chk.pack(side=Tkinter.LEFT)
>>> var.get()  #unchecked
0
>>> var.get()  #checked
1

回答by bitsmack

If you use the new*ttk module from tkinter, you can read and write checkbutton states without assigning variables.

如果您使用tkinter的新*ttk 模块,您可以在不分配变量的情况下读取和写入检查按钮状态。

import tkinter
from tkinter import ttk
tkwindow = tkinter.Tk()
chk = ttk.Checkbutton(tkwindow, text="foo")
chk.grid(column=0, row=0)

Notice that the new checkbox defaults to the "alternate", sometimes called "half-checked", state:

请注意,新复选框默认为“alternate”,有时称为“half-checked”,状态:

alternate

备用

You can read the current state usinge the .state() method:

您可以使用 .state() 方法读取当前状态:

>>> print(chk.state())  # half-checked
('alternate',)
>>> print(chk.state())  # checked
('selected',)
>>> print(chk.state())  # not checked
()

To set the state in code:

在代码中设置状态:

chk.state(['selected'])  # check the checkbox
chk.state(['!selected']) # clear the checkbox
chk.state(['disabled'])  # disable the checkbox
chk.state(['!disabled','selected']) # enable the checkbox and put a check in it!

And here is a convenient way to check for a specific state:

这是检查特定状态的便捷方法:

chk.instate(['selected'])  # returns True if the box is checked


There are two tricky things I've found:

我发现了两件棘手的事情:

  1. The initial state is "alternate", and this state flag doesn't get cleared when adding a "selected" state flag. So, if you want to toggle your checkbutton in code, you'll first need to clear the "alternate" flag:

    chk.state(['!alternate'])
    
  2. If you disable/enable the checkbutton using

    chk.state(['disabled'])
    chk.state(['!disabled'])
    

    then everything works fine. But, if you use these common, alternate methods:

    chk.config(state=tk.DISABLED)
    chk.config(state=tk.NORMAL)
    

    then it reasserts the 'alternate' flag.

    This behavior doesn't happen if you assign a variable to the checkbutton, but then, if you wanted to assign a variable then this answer probably won't help you :)

  1. 初始状态是“alternate”,当添加“selected”状态标志时,这个状态标志不会被清除。所以,如果你想在代码中切换你的复选按钮,你首先需要清除“alternate”标志:

    chk.state(['!alternate'])
    
  2. 如果您禁用/启用复选按钮使用

    chk.state(['disabled'])
    chk.state(['!disabled'])
    

    然后一切正常。但是,如果您使用这些常见的替代方法:

    chk.config(state=tk.DISABLED)
    chk.config(state=tk.NORMAL)
    

    然后它重新声明“替代”标志。

    如果您为复选按钮分配一个变量,则不会发生这种行为,但是,如果您想分配一个变量,那么这个答案可能对您没有帮助:)



* ttk became available in Python 2.7 (Tk 8.5). This questiontalks about the differences between the old standard widgets and the newer, "themed" ones.

* ttk 在 Python 2.7 (Tk 8.5) 中可用。这个问题讨论了旧标准小部件和较新的“主题”小部件之间的差异。