我可以让 tkinter 中的一个复选按钮检查所有其他复选按钮吗?
Can i make one checkbutton in tkinter check all the other checkbuttons?
有什么方法可以让我在 tkinter 中有一个复选按钮,如果选中它,也会检查所有其他复选按钮吗?
示例:
Checkbutton(root, text="A").grid(row=0, column=0, sticky=W)
Checkbutton(root, text="B").grid(row=1, column=0, sticky=W)
Checkbutton(root, text="C").grid(row=2, column=0, sticky=W)
Checkbutton(root, text="ABC").grid(row=3, column=0, sticky=W)
因此,如果您选中 ABC 按钮,那么所有其他按钮也会被选中。有什么办法可以完成我想要的吗?
Python: 3.4
OS: Windows
给定一个 Checkbutton
:
check = Checkbutton(root, text="Checkbutton",...)
check.grid(row=0, column=0, ...)
取消选中使用:check.deselect()
或(重新)检查:check.select()
创建 A、B 和 C 复选框时,将它们保存在列表中;然后,当点击 ABC 时,您可以遍历列表并检查它们:
A = Checkbutton(root, text="A")
B = Checkbutton(root, text="B")
C = Checkbutton(root, text="C")
cbs = [A, B, C]
A.grid(row=0, column=0, sticky=W)
B.grid(row=1, column=0, sticky=W)
C.grid(row=2, column=0, sticky=W)
def checkall():
for cb in cbs:
cb.select()
Checkbutton(root, text="ABC", command=checkall).grid(row=3, column=0, sticky=W)
更完整的例子,不妨参考下面的代码。它演示了一种可以采用的方法,让复选框部分确定另一个的值。由于单个和组复选框之间的逻辑略有不同,因此使用了两个不同的事件处理程序。
#! /usr/bin/env python3
from tkinter import *
from tkinter.ttk import *
class Application(Frame):
@classmethod
def main(cls):
# Create a test environment to show how the class works.
NoDefaultRoot()
root = Tk()
root.title('Demonstration')
root.resizable(False, False)
root.minsize(250, 100)
frame = cls(root)
frame.grid()
root.mainloop()
def __init__(self, master=None, **kw):
super().__init__(master, **kw)
# Create variables for the checkboxes.
self.bv_a = BooleanVar(self)
self.bv_b = BooleanVar(self)
self.bv_c = BooleanVar(self)
self.bv_abc = BooleanVar(self)
self.cb_variables = self.bv_a, self.bv_b, self.bv_c
# Create each of the desired checkboxes.
options = dict(
master=self, command=self.update_any, onvalue=True, offvalue=False
)
self.cb_a = Checkbutton(text='A', variable=self.bv_a, **options)
self.cb_b = Checkbutton(text='B', variable=self.bv_b, **options)
self.cb_c = Checkbutton(text='C', variable=self.bv_c, **options)
options.update(command=self.update_all)
self.cb_abc = Checkbutton(text='ABC', variable=self.bv_abc, **options)
# Make sure the checkboxes are displayed.
self.cb_a.grid()
self.cb_b.grid()
self.cb_c.grid()
self.cb_abc.grid()
def update_any(self):
# Only check "ABC" if all the other boxes are checked.
self.bv_abc.set(all(variable.get() for variable in self.cb_variables))
def update_all(self):
# Copy the status of "ABC" to all of the other boxes.
for variable in self.cb_variables:
variable.set(self.bv_abc.get())
if __name__ == '__main__':
Application.main()
只需将它们全部绑定到同一个变量即可:
var = IntVar()
Checkbutton(root, text="A", variable=var).grid(row=0, column=0, sticky=W)
Checkbutton(root, text="B", variable=var).grid(row=1, column=0, sticky=W)
Checkbutton(root, text="C", variable=var).grid(row=2, column=0, sticky=W)
Checkbutton(root, text="ABC", variable=var).grid(row=3, column=0, sticky=W)
有什么方法可以让我在 tkinter 中有一个复选按钮,如果选中它,也会检查所有其他复选按钮吗?
示例:
Checkbutton(root, text="A").grid(row=0, column=0, sticky=W)
Checkbutton(root, text="B").grid(row=1, column=0, sticky=W)
Checkbutton(root, text="C").grid(row=2, column=0, sticky=W)
Checkbutton(root, text="ABC").grid(row=3, column=0, sticky=W)
因此,如果您选中 ABC 按钮,那么所有其他按钮也会被选中。有什么办法可以完成我想要的吗?
Python: 3.4
OS: Windows
给定一个 Checkbutton
:
check = Checkbutton(root, text="Checkbutton",...)
check.grid(row=0, column=0, ...)
取消选中使用:check.deselect()
或(重新)检查:check.select()
创建 A、B 和 C 复选框时,将它们保存在列表中;然后,当点击 ABC 时,您可以遍历列表并检查它们:
A = Checkbutton(root, text="A")
B = Checkbutton(root, text="B")
C = Checkbutton(root, text="C")
cbs = [A, B, C]
A.grid(row=0, column=0, sticky=W)
B.grid(row=1, column=0, sticky=W)
C.grid(row=2, column=0, sticky=W)
def checkall():
for cb in cbs:
cb.select()
Checkbutton(root, text="ABC", command=checkall).grid(row=3, column=0, sticky=W)
更完整的例子,不妨参考下面的代码。它演示了一种可以采用的方法,让复选框部分确定另一个的值。由于单个和组复选框之间的逻辑略有不同,因此使用了两个不同的事件处理程序。
#! /usr/bin/env python3
from tkinter import *
from tkinter.ttk import *
class Application(Frame):
@classmethod
def main(cls):
# Create a test environment to show how the class works.
NoDefaultRoot()
root = Tk()
root.title('Demonstration')
root.resizable(False, False)
root.minsize(250, 100)
frame = cls(root)
frame.grid()
root.mainloop()
def __init__(self, master=None, **kw):
super().__init__(master, **kw)
# Create variables for the checkboxes.
self.bv_a = BooleanVar(self)
self.bv_b = BooleanVar(self)
self.bv_c = BooleanVar(self)
self.bv_abc = BooleanVar(self)
self.cb_variables = self.bv_a, self.bv_b, self.bv_c
# Create each of the desired checkboxes.
options = dict(
master=self, command=self.update_any, onvalue=True, offvalue=False
)
self.cb_a = Checkbutton(text='A', variable=self.bv_a, **options)
self.cb_b = Checkbutton(text='B', variable=self.bv_b, **options)
self.cb_c = Checkbutton(text='C', variable=self.bv_c, **options)
options.update(command=self.update_all)
self.cb_abc = Checkbutton(text='ABC', variable=self.bv_abc, **options)
# Make sure the checkboxes are displayed.
self.cb_a.grid()
self.cb_b.grid()
self.cb_c.grid()
self.cb_abc.grid()
def update_any(self):
# Only check "ABC" if all the other boxes are checked.
self.bv_abc.set(all(variable.get() for variable in self.cb_variables))
def update_all(self):
# Copy the status of "ABC" to all of the other boxes.
for variable in self.cb_variables:
variable.set(self.bv_abc.get())
if __name__ == '__main__':
Application.main()
只需将它们全部绑定到同一个变量即可:
var = IntVar()
Checkbutton(root, text="A", variable=var).grid(row=0, column=0, sticky=W)
Checkbutton(root, text="B", variable=var).grid(row=1, column=0, sticky=W)
Checkbutton(root, text="C", variable=var).grid(row=2, column=0, sticky=W)
Checkbutton(root, text="ABC", variable=var).grid(row=3, column=0, sticky=W)