How to create a button to select all checkbuttons
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()
Replace:
Button(root, text = 'all', command = select_all()).pack()
with:
Button(root, text='all', command=select_all).pack()
Checkbutton(root, text = i).pack()
returns you None
. So you are actually appending None
s to your list. What you need to do is: append the instance of Button
not the value which .pack()
returns (None
)