如何创建一个按钮来选择所有的复选按钮

Abi*_*Abi 4 python checkbox tkinter list

我想用 TkInter 在 Python 中创建一个复选框列表,并尝试用一个按钮选择所有复选框。

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()
Run Code Online (Sandbox Code Playgroud)

我担心他没有填写清单cbuts

cbuts.append(Checkbutton(root, text = i).pack())
Run Code Online (Sandbox Code Playgroud)

Abi*_*Abi 5

这是正确的代码

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()
Run Code Online (Sandbox Code Playgroud)