tkinterを使用して8 x 8のボタンマトリックスを作成しています。個々のボタンを押したときに最後のリストに追加されます(例:finalList =((0,0)、(5,7)、 6,6)、...)、私はすぐに8x8(x、y)座標の画像を作成することができます。ボタンを使ってウィンドウを作成しましたが、これらのボタンを関数で参照してボタンの色を変更、一覧表示、または色を変更することもできます。ボタンをクリックして解決策を見つけようとしています。tkinter複数のボタンの色の変更
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()
助けてくれてありがとう、あなたの2番目の例を使用し、それは魅力を働いた。本当に助けを感謝し、私は自分自身の残りのアイデアを管理することを希望します。これから明確に学べます! – SgtSafety