win=Tk()
level1=matrixmaker(4)
def display(x,y):
if z["text"] == " ":
# switch to Goodbye
z["text"] = level1[x][y]
else:
# reset to Hi
z["text"] = " "
for x in range(0,4):
for y in range(0,4):
z=Button(win,text=" ", command=display(x,y))
z.grid(row=x,column=y)
我有这段代码但我不知道如何让显示功能工作。如何在不具有硬编码变量名称的情况下调用按钮并更改文本?更改tkinter内的按钮
在Tkinter中,'command'参数是_reference_函数,而不是函数本身。你必须将参数变成没有参数的'command = display'。看到这里:http://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter –
此外,你只有两个参数调用三个参数函数(无论如何都不能添加)。 –
使用'command = lambda a = x,b = y:display(a,b)' – furas