Reputation: 205
Is there any way i can have one checkbutton in tkinter which if checked, will check every other checkbutton aswell?
Example:
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)
So if you would check the ABC button, all the other buttons would get checked aswell. Is there any way to accomplish what i want?
Python: 3.4
OS: Windows
Upvotes: 2
Views: 4551
Reputation: 22041
For a more complete example, you may wish to refer to the following code. It demonstrates one approach one could take in having checkboxes partially determine the value of another. Since the logic is slightly different between single and group checkboxes, two different event handlers are used.
#! /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()
Upvotes: 0
Reputation: 13729
Just tie them all to the same variable:
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)
Upvotes: 0
Reputation: 12721
As you create the A, B, and C checkboxes, save them in a list; then, when ABC is clicked, you can iterate over the list and check them all:
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)
Upvotes: 2
Reputation: 1219
Given a Checkbutton
:
check = Checkbutton(root, text="Checkbutton",...)
check.grid(row=0, column=0, ...)
Uncheck it using: check.deselect()
Or (re-)check it with: check.select()
Upvotes: 0