【发布时间】:2013-05-03 23:12:15
【问题描述】:
这是我的第一个 Python 程序,我认为我的 if 语句是正确的,我可能不知道,我不知道。我想要做的是,当单击 Tkinter 按钮时,我想要调用的函数来检查按钮上显示的图像,然后相应地更改其图像。
这是我的函数代码:
def update_binary_text(first,second):
if buttonList[first][second]["image"] == photo:
buttonList[first][second]["image"] = photo1
这是带有命令的 for 循环 [2d 按钮列表]:
for i in range (0,number):
buttonList.append([])
for j in range(0,number):
print(i,j)
buttonList[i].append(Button(game, borderwidth=0,highlightthickness=0, image=photo,command = lambda i=i, j=j: update_binary_text(i,j)))
buttonList[i][j].grid(row=i*20,column=j*20)
问题是,当我运行它时,它可以正常打开,但是当我单击所有按钮时,什么也没有发生。如果我把 if 语句取出来,只做赋值,它会起作用,但我需要检查先显示哪个图像。
有人有解决办法吗?
我刚刚遇到了另一个问题。我之前收到的解决方案效果很好,并且更改了图像,但仅在第一次单击时。之后就再也不会变了。
代码如下:
def update_binary_text(first,second):
#print("Called")
if buttonList[first][second].image == photo:
buttonList[first][second]["image"] = photo0
elif buttonList[first][second].image == photo0:
buttonList[first][second]["image"] = photo1
发生的情况是,当我第一次单击任何按钮时,它会从空白按钮变为带有图像的按钮,当我再次单击它时,它应该会更改其图像,但事实并非如此。如果有人想看这里是初始化photo、photo0和photo1的语句:
photo = PhotoImage(file ="blank.gif")
photo0 = PhotoImage(file="0.gif")
photo1 = PhotoImage(file="1.gif")
【问题讨论】:
标签: button python-3.x tkinter conditional