我使用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()