我使用tkinter创建一个8x8按钮矩阵,当按下单个按钮时,它会添加到最终列表中(例如finalList=((0,0),(5,7),(6,6),…),允许我快速创建8x8(x,y)坐标图像。我已经创建了一个带有按钮的窗口,但是现在尝试在一个函数中引用这些按钮以添加到列表中甚至更改按钮的颜色时出现问题
我已经读到,一旦按钮被创建,你创建了另一个,它将移动到按钮引用。我怀疑我需要使用dict或2D数组来存储所有这些按钮的引用,但我正在努力寻找解决方案。在from tkinter import *
class App:
def updateChange(self):
'''
-Have the button change colour when pressed
-add coordinate to final list
'''
x , y = self.xY
self.buttons[x][y].configure(bg="#000000")
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.buttons = [] # Do I need to create a dict of button's so I can reference the particular button I wish to update?
for matrixColumn in range(8):
for matrixRow in range(8):
self.xY = (matrixColumn,matrixRow)
stringXY = str(self.xY)
self.button = Button(frame,text=stringXY, fg="#000000", bg="#ffffff", command = self.updateChange).grid(row=matrixRow,column=matrixColumn)
self.buttons[matrixColumn][matrixRow].append(self.button)
root = Tk()
app = App(root)
root.mainloop()