python tkinter 如何更改特定网格上小部件的标签 space?
python tkinter how to change the label of a widget on a particular grid space?
我有一个 3x3 的按钮网格,所有按钮都绑定到相同的事件函数。此函数根据单击的按钮更改特定按钮的标签。我希望能够通过查看按钮在网格上的位置(即行值和列值)来选择将影响哪些按钮。
例如,我希望能够说“考虑到点击的按钮是打开的(第 1 行,第 2 列),我想更改(第 2 行,第 0 列)和(行1,第 1 列)。
我知道如何找到被点击按钮的行和列:
import tkinter
def click(event):
space = event.widget
space_label = space['text']
row = space.grid_info()['row'] #get the row of clicked button
column = space.grid_info()['column'] #get the column of clicked button
board = tkinter.Tk()
for i in range(0,9): #creates the 3x3 grid of buttons
button = tkinter.Button(text = "0")
if i in range(0,3):
r = 0
elif i in range(3,6):
r = 1
else:
r = 2
button.grid(row = r, column = i%3)
button.bind("<Button-1>",click)
board.mainloop()
但我不知道如何访问仅给定网格的一行和一列的按钮标签。
P.S。编码新手,很抱歉,如果这很明显,我环顾四周但找不到足够相似的问题。
如果您打算在创建小部件并将其添加到您的界面后对它执行任何操作,最好保留一个参考。
这里是对您的代码的修改,它创建了一个字典 button_dict
来存储您的每个按钮。键是元组 (row,column)
。我将 button_dict
作为附加输入添加到您的 click
函数中,并使用 lambda
.
修改了按钮绑定以包含此新输入
import tkinter
def click(event,button_dict):
space = event.widget
space_label = space['text']
row = space.grid_info()['row'] #get the row of clicked button
column = space.grid_info()['column'] #get the column of clicked button
button = button_dict[(row,column)] # Retrieve our button using the row/col
print(button['text']) # Print button text for demonstration
board = tkinter.Tk()
button_dict = {} # Store your button references here
for i in range(0,9): #creates the 3x3 grid of buttons
button = tkinter.Button(text = i) # Changed the text for demonstration
if i in range(0,3):
r = 0
elif i in range(3,6):
r = 1
else:
r = 2
button.grid(row = r, column = i%3)
button.bind("<Button-1>",lambda event: click(event,button_dict))
button_dict[(r,i%3)] = button # Add reference to your button dictionary
board.mainloop()
如果您只对按下的按钮感兴趣,您可以简单地访问 event.widget
属性。从您的示例来看,您似乎想在每个事件上修改任意数量的按钮,因此我认为该词典将为您提供更多功能。
我有一个 3x3 的按钮网格,所有按钮都绑定到相同的事件函数。此函数根据单击的按钮更改特定按钮的标签。我希望能够通过查看按钮在网格上的位置(即行值和列值)来选择将影响哪些按钮。
例如,我希望能够说“考虑到点击的按钮是打开的(第 1 行,第 2 列),我想更改(第 2 行,第 0 列)和(行1,第 1 列)。
我知道如何找到被点击按钮的行和列:
import tkinter
def click(event):
space = event.widget
space_label = space['text']
row = space.grid_info()['row'] #get the row of clicked button
column = space.grid_info()['column'] #get the column of clicked button
board = tkinter.Tk()
for i in range(0,9): #creates the 3x3 grid of buttons
button = tkinter.Button(text = "0")
if i in range(0,3):
r = 0
elif i in range(3,6):
r = 1
else:
r = 2
button.grid(row = r, column = i%3)
button.bind("<Button-1>",click)
board.mainloop()
但我不知道如何访问仅给定网格的一行和一列的按钮标签。
P.S。编码新手,很抱歉,如果这很明显,我环顾四周但找不到足够相似的问题。
如果您打算在创建小部件并将其添加到您的界面后对它执行任何操作,最好保留一个参考。
这里是对您的代码的修改,它创建了一个字典 button_dict
来存储您的每个按钮。键是元组 (row,column)
。我将 button_dict
作为附加输入添加到您的 click
函数中,并使用 lambda
.
import tkinter
def click(event,button_dict):
space = event.widget
space_label = space['text']
row = space.grid_info()['row'] #get the row of clicked button
column = space.grid_info()['column'] #get the column of clicked button
button = button_dict[(row,column)] # Retrieve our button using the row/col
print(button['text']) # Print button text for demonstration
board = tkinter.Tk()
button_dict = {} # Store your button references here
for i in range(0,9): #creates the 3x3 grid of buttons
button = tkinter.Button(text = i) # Changed the text for demonstration
if i in range(0,3):
r = 0
elif i in range(3,6):
r = 1
else:
r = 2
button.grid(row = r, column = i%3)
button.bind("<Button-1>",lambda event: click(event,button_dict))
button_dict[(r,i%3)] = button # Add reference to your button dictionary
board.mainloop()
如果您只对按下的按钮感兴趣,您可以简单地访问 event.widget
属性。从您的示例来看,您似乎想在每个事件上修改任意数量的按钮,因此我认为该词典将为您提供更多功能。