【发布时间】:2020-03-07 08:10:58
【问题描述】:
我正在制作一个测验类型的程序,其中我添加了一个与其他问题不同的奖励问题。在这里,你有 8 个按钮,问题是:
使用给定选项中的四个按钮弹出列表“lst”中的所有元素
按钮是:
["lst", "while", "for", "i", ":", "lst.pop()", "in", "range(lst)"]
我想要做的是在点击后禁用每个按钮,并在点击 4 次后禁用所有按钮。
我也想知道,如何检查点击的顺序是否正确?
我创建了一个禁用索引为“索引”的按钮的函数
def disable(buttons, index, word):
buttons[index].config(state="disabled")
然后我在一个循环中创建了 8 个按钮。
words=["lst", "while", "for", "i", ":", "lst.pop()", "in", "range(lst)"]
buttons = []
for index in range(8):
n = words[index]
button = Button(root, text = n, command = lambda index = index, n = n: disable(buttons, index, n)).pack(side = 'left')
buttons.append(button)
这是出现的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "<ipython-input-163-f56fc3dd64da>", line 340, in <lambda>
button = Button(root, text = n, command = lambda index = index, n = n: disable(buttons, index, n)).pack(side = 'left')
File "<ipython-input-163-f56fc3dd64da>", line 353, in disable
buttons[index].config(state="disabled")
AttributeError: 'NoneType' object has no attribute 'config'
【问题讨论】:
标签: python tkinter radio-button