I want to create a list of checkboxes in Python with TkInter and try to select all checkboxes with a button.
from tkinter import *
def create_cbuts():
for i in cbuts_text:
cbuts.append(Checkbutton(root, text = i).pack())
def select_all():
for j in cbuts:
j.select()
root = Tk()
cbuts_text = ['a','b','c','d']
cbuts = []
create_cbuts()
Button(root, text = 'all', command = select_all()).pack()
mainloop()
I fear that he does not fill the list cbuts
:
cbuts.append(Checkbutton(root, text = i).pack())
Here is the correct code
from tkinter import *
def create_cbuts():
for index, item in enumerate(cbuts_text):
cbuts.append(Checkbutton(root, text = item))
cbuts[index].pack()
def select_all():
for i in cbuts:
i.select()
def deselect_all():
for i in cbuts:
i.deselect()
root = Tk()
cbuts_text = ['a','b','c','d']
cbuts = []
create_cbuts()
Button(root, text = 'all', command = select_all).pack()
Button(root, text = 'none', command = deselect_all).pack()
mainloop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With