Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a button to select all checkbuttons

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())
like image 606
Abi Avatar asked Feb 20 '23 20:02

Abi


1 Answers

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()
like image 157
Abi Avatar answered Feb 22 '23 11:02

Abi