python tkinter如何更改特定网格空间上小部件的标签?
我有一个3x3的按钮网格,所有按钮都绑定到同一个事件函数。此函数根据单击的按钮更改特定按钮的标签。我希望能够通过查看按钮在网格上的位置,即行和列值来选择将影响哪些按钮 例如,我想说“考虑到按钮被点击(第1行,第2列),我想更改(第2行,第0列)和(第1行,第1列)上按钮的标签 我知道如何查找单击的按钮的行和列:python tkinter如何更改特定网格空间上小部件的标签?,python,button,tkinter,grid,Python,Button,Tkinter,Grid,我有一个3x3的按钮网格,所有按钮都绑定到同一个事件函数。此函数根据单击的按钮更改特定按钮的标签。我希望能够通过查看按钮在网格上的位置,即行和列值来选择将影响哪些按钮 例如,我想说“考虑到按钮被点击(第1行,第2列),我想更改(第2行,第0列)和(第1行,第1列)上按钮的标签 我知道如何查找单击的按钮的行和列: import tkinter def click(event): space = event.widget space_label = space['text']
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()
导入tkinter
def单击(事件):
space=event.widget
空格\标签=空格['text']
row=space.grid_info()['row']#获取单击按钮的行
column=space.grid_info()
board=tkinter.Tk()
对于范围(0,9)内的i:#创建3x3的按钮网格
按钮=tkinter.button(text=“0”)
如果i在范围(0,3)内:
r=0
elif i在范围(3,6)内:
r=1
其他:
r=2
button.grid(行=r,列=i%3)
按钮。绑定(“,单击)
board.mainloop()
但我不知道如何访问只有一行和一列网格的按钮标签
另外,对编码不熟悉,如果这是显而易见的,很抱歉,我四处查看了一下,但没有找到足够多的类似问题。如果您计划在创建小部件并将其添加到界面后对其进行任何操作,最好保留一个参考 下面是对代码的修改,它创建了一个字典
按钮\u dict
来存储每个按钮。键是元组(行、列)
。我添加了按钮作为单击
功能的附加输入,并使用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()
导入tkinter
def点击(事件、按钮):
space=event.widget
空格\标签=空格['text']
row=space.grid_info()['row']#获取单击按钮的行
column=space.grid_info()
button=button_dict[(行、列)]#使用行/列检索我们的按钮
打印(按钮[文本])#打印按钮文本以供演示
board=tkinter.Tk()
button_dict={}#在此处存储您的按钮引用
对于范围(0,9)内的i:#创建3x3的按钮网格
button=tkinter.button(text=i)#更改了演示文本
如果i在范围(0,3)内:
r=0
elif i在范围(3,6)内:
r=1
其他:
r=2
button.grid(行=r,列=i%3)
button.bind(“,lambda事件:单击(事件,按钮)
button_dict[(r,i%3)]=按钮#添加对按钮字典的引用
board.mainloop()
如果您只对按下的按钮感兴趣,您可以简单地访问事件.widget
属性。从您的示例中,听起来您希望修改每个事件上的任意数量的按钮,因此我认为字典将为您提供更多的多功能性。如果您计划在创建并添加小部件后对其执行任何操作将它添加到您的界面中,最好保留一个引用
下面是对代码的修改,它创建了一个字典按钮\u dict
来存储每个按钮。键是元组(行、列)
。我添加了按钮作为单击
功能的附加输入,并使用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()
导入tkinter
def点击(事件、按钮):
space=event.widget
空格\标签=空格['text']
row=space.grid_info()['row']#获取单击按钮的行
column=space.grid_info()
button=button_dict[(行、列)]#使用行/列检索我们的按钮
打印(按钮[文本])#打印按钮文本以供演示
board=tkinter.Tk()
button_dict={}#在此处存储您的按钮引用
对于范围(0,9)内的i:#创建3x3的按钮网格
button=tkinter.button(text=i)#更改了演示文本
如果i在范围(0,3)内:
r=0
elif i在范围(3,6)内:
r=1
其他:
r=2
button.grid(行=r,列=i%3)
button.bind(“,lambda事件:单击(事件,按钮)
button_dict[(r,i%3)]=按钮#添加对按钮字典的引用
board.mainloop()
如果您只对按下的按钮感兴趣,您可以简单地访问event.widget
属性。从您的示例中,听起来您想修改每个事件上的任意数量的按钮,因此我认为字典将为您提供更多的功能性。这是否回答了您的问题?我是编程新手,所以我不知道mu关于lambda函数或网格从属函数,但这在理论上似乎回答了这个问题,所以谢谢!这回答了你的问题吗?我是编程新手,所以我对lambda函数或网格从属函数不太了解,但这在理论上似乎回答了这个问题,所以谢谢!使用像d这样的附加序列是一种反模式ict
谢谢,实际上我以前从未听过这个词。我查过它,我理解了它的基本原理。我会继续努力避免它。撇开反模式不谈,我相信这解决了我的问题,谢谢!使用像dict
这样的附加序列是一个反模式。谢谢,我以前从未听过这个词。我看了看我t up,我理解基本原理。我将继续努力避免它。撇开反模式不谈,我相信这解决了我的问题,谢谢!