我想用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()
我担心他没有填写列表cbuts:
cbuts.append(Checkbutton(root, text = i).pack())
解决方法:
更换:
Button(root, text = 'all', command = select_all()).pack()
有:
Button(root, text='all', command=select_all).pack()
Checkbutton(root,text = i).pack()返回None.所以你实际上是将Nones附加到你的列表中.你需要做的是:追加Button的实例而不是.pack()返回的值(None)
标签:python,list,checkbox,tkinter
来源: https://codeday.me/bug/20190826/1727929.html