Python 更新按钮矩阵中的按钮文本(Tkinter)
我有一个16x16矩阵的tkinter按钮:Python 更新按钮矩阵中的按钮文本(Tkinter),python,python-3.x,user-interface,tkinter,Python,Python 3.x,User Interface,Tkinter,我有一个16x16矩阵的tkinter按钮: for c in range(self.track_size): row = [] for r in range(self.track_size): button = tk.Button( self.ground_data_frame, text="0" ) button.grid(row=r, column=c, sticky=tk.N
for c in range(self.track_size):
row = []
for r in range(self.track_size):
button = tk.Button(
self.ground_data_frame,
text="0"
)
button.grid(row=r, column=c, sticky=tk.NW+tk.NE+tk.SW+tk.SE+tk.W+tk.E+tk.N+tk.S)
row.append(button)
self.ground_data_buttons.append(row)
我为每个人定义了一个命令函数后定义:
for r in self.ground_data_buttons:
for b in r:
b.config(command=lambda: b.config(text=self.ground_data_values[
self.values_list_box.get(tk.ACTIVE)
]))
想法是用列表框中选择的值更新单击的按钮文本。问题是,例如,当我单击第一个按钮时,会发生以下情况:
它只更新最后一个按钮。它完全忽略前面所有按钮的命令。不使用循环来手动创建所有按钮是荒谬的。为什么会发生这种情况
编辑:即使我尝试使用tk.StringVar()
for c in range(self.track_size):
row = []
for r in range(self.track_size):
button_text = tk.StringVar()
button = tk.Button(
self.ground_data_frame,
textvariable=button_text
)
button.grid(row=r, column=c, sticky=tk.NW+tk.NE+tk.SW+tk.SE+tk.W+tk.E+tk.N+tk.S)
row.append((button, button_text))
self.ground_data_buttons.append(row)
for r in self.ground_data_buttons:
for b in r:
b[0].config(command=lambda: b[1].set(self.ground_data_values[
self.values_list_box.get(tk.ACTIVE)
]))
class Button(tk.Button):
def __init__(self, frame):
self.textvariable = tk.StringVar()
tk.Button.__init__(self, frame, textvariable=self.textvariable)
self.update_value("0")
def update_value(self, value):
self.textvariable.set(value)
def set_command(self, f):
self.config(command=f)
EDIT2:即使使用类,问题仍然存在。
for c in range(self.track_size):
row = []
for r in range(self.track_size):
button_text = tk.StringVar()
button = tk.Button(
self.ground_data_frame,
textvariable=button_text
)
button.grid(row=r, column=c, sticky=tk.NW+tk.NE+tk.SW+tk.SE+tk.W+tk.E+tk.N+tk.S)
row.append((button, button_text))
self.ground_data_buttons.append(row)
for r in self.ground_data_buttons:
for b in r:
b[0].config(command=lambda: b[1].set(self.ground_data_values[
self.values_list_box.get(tk.ACTIVE)
]))
class Button(tk.Button):
def __init__(self, frame):
self.textvariable = tk.StringVar()
tk.Button.__init__(self, frame, textvariable=self.textvariable)
self.update_value("0")
def update_value(self, value):
self.textvariable.set(value)
def set_command(self, f):
self.config(command=f)
for c in range(self.track_size):
row = []
for r in range(self.track_size):
button = Button(self.ground_data_frame)
button.grid(row=r, column=c, sticky=tk.NW+tk.NE+tk.SW+tk.SE+tk.W+tk.E+tk.N+tk.S)
row.append(button)
self.ground_data_buttons.append(row)
for r in self.ground_data_buttons:
for b in r:
b.set_command(lambda: b.update_value(self.ground_data_values[self.values_list_box.get(tk.ACTIVE)]))
使用answer作为指导,解决方案是在lambda
回调函数中向变量添加默认值。在您的情况下,它看起来像这样:
def updateButton(row, column):
buttons[row][column].config(text="1")
for row in range(len(buttons)):
for col in range(len(buttons[row])):
buttons[row][col].config(command=lambda row=row, col=col: updateButton(row, col))
您可以创建一个包含
按钮和updateValue()
函数的类,这是一个有趣的想法!我将尝试一下,看看它是否正确地更新了按钮。检查编辑,即使使用类,问题也是一样的。我已经这样解决了:b.config(command=lambda b=b:b.config(text=self.ground\u data\u values[self.values\u list\u box.get(tk.ACTIVE)]
。是的,这就是问题所在。哎呀,这个收尾细节真的很难记住。非常感谢。