4

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())
4

2 に答える 2

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()
于 2012-07-15T17:17:08.500 に答える
3

交換:

Button(root, text = 'all', command = select_all()).pack()

と:

Button(root, text='all', command=select_all).pack()

Checkbutton(root, text = i).pack()あなたを返しますNone。したがって、実際にはNoneリストに s を追加しています。あなたがする必要があるのは:()を返すButton値ではなくのインスタンスを追加することです.pack()None

于 2012-07-15T08:23:01.693 に答える