【发布时间】:2016-11-07 19:24:18
【问题描述】:
我正在使用 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()
【问题讨论】:
标签: python python-3.x button tkinter command