【发布时间】:2018-04-27 23:44:42
【问题描述】:
我正在尝试实现一个名为“五连冠”的游戏。 我创建了一个 15×15 的列表来放置按钮。 (我使用了range(16),因为我还想要一行一列来显示行号和列号)
我希望我的实现就像当一个按钮被点击时,它变成了一个标签。 但我不知道用户点击了哪个按钮。
我该如何实现呢?谢谢!
from tkinter import *
root=Tk()
root.wm_title("Five In a Row")
buttonlst=[ list(range(16)) for i in range(16)]
chess=Label(root,width=2,height=2,text='0')
def p(button):
gi=button.grid_info()
x=gi['row']
y=gi['column']
button.grid_forget()
chess.grid(row=x,column=y)
buttonlst[x][y]=chess
for i in range(16):
for j in range(16):
if i==0:
obj=Label(root,width=2,text=hex(j)[-1].upper())
elif j==0:
obj=Label(root,width=2,text=hex(i)[-1].upper())
else:
obj=Button(root,relief=FLAT,width=2,command=p(obj))
obj.grid(row=i,column=j)
buttonlst[i][j]=obj
root.mainloop()
有一个类似的问题How to determine which button is pressed out of Button grid in Python TKinter?。但我不太明白。
【问题讨论】:
-
这看起来与您链接到的问题完全相同。你没有得到答案的哪一部分?你能说得更具体点吗?
-
@BryanOakley 我不知道他如何存储按钮的信息。
-
如果你想确定哪个按钮被按下,你可以有一个函数,至少将按钮位置信息作为参数。