我是Python新手,我正在尝试使用Tkinter创建一个简单的应用程序
def appear(x):
return lambda: results.insert(END, x)
letters=["A", "T", "D", "M", "E", "A", "S", "R", "M"]
for index in range(9):
n=letters[index]
nButton = Button(buttons, bg="White", text=n, width=5, height=1,
command =appear(n), relief=GROOVE).grid(padx=2, pady=2, row=index%3,
column=index/3)
我要做的是在单击按钮后禁用它们。 我试过了
def appear(x):
nButton.config(state="disabled")
return lambda: results.insert(END, x)
但它给了我以下错误:
NameError: global name 'nButton' is not defined
这里有几个问题:
无论何时动态创建小部件,都需要在集合中存储对它们的引用,以便以后可以访问它们
Tkinter小部件的
grid
方法始终返回None
。因此,您需要将对grid
的任何调用放在它们自己的线路上每当您将按钮的} 等来“隐藏”该函数的调用,直到单击按钮为止。有关更多信息,请参见https://stackoverflow.com/a/20556892/2555451
command
选项分配给需要参数的函数时,必须使用^{下面是解决所有这些问题的示例脚本:
这对我目前正在进行的一项工作非常有帮助,添加了一个小的修正
相关问题 更多 >
编程相关推荐