如何使用嵌套的名称列表来标识我在网格上单击的按钮?Python-.Tkinter

如何使用嵌套的名称列表来标识我在网格上单击的按钮?Python-.Tkinter,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我目前有一个带有行和列的按钮网格(使用for循环),希望能够单击其中一个按钮,然后返回,例如,单击a1。 我希望它返回给我哪个按钮被点击。 我已经试着让它打印出来,但我不能确定它显示的按钮,也就是说。!按钮25点击 在red\u click中有pairr,c这样你就可以得到seats[r][c]在中有pairr,c这样你就可以得到seats[r][c]你可以把值绑定到按钮对象上的任意属性。然后可以使用属性查找检索该值 此外,最好使用座位列表中保存的座位计划来创建按钮。您可以使用来帮助迭代这些

我目前有一个带有行和列的按钮网格(使用for循环),希望能够单击其中一个按钮,然后返回,例如,单击a1。

我希望它返回给我哪个按钮被点击。
我已经试着让它打印出来,但我不能确定它显示的按钮,也就是说。!按钮25点击

red\u click
中有pair
r,c
这样你就可以得到
seats[r][c]
中有pair
r,c
这样你就可以得到
seats[r][c]

你可以把值绑定到
按钮
对象上的任意属性。然后可以使用属性查找检索该值

此外,最好使用
座位列表中保存的座位计划来创建按钮。您可以使用来帮助迭代这些值,同时仍然可以访问网格所需的索引:

import tkinter as tk

seats= [["A", "a1", "a2", "a3", "a4", "a5"],
        ["B", "b1", "b2", "b3", "b4", "b5"],
        ["C", "c1", "c2", "c3", "c4", "c5"],
        ["D", "d1", "d2", "d3", "d4", "d5"]]

def make_buttons():
    for r, row in enumerate(seats):
        for c, seat in enumerate(row[1:]):        # N.B. skip first element
            btn = tk.Button(root, text="Empty")
            btn.name = seat                       # arbitrary attribute on Button object
            btn['command'] = lambda c=c, r=r, b=btn:red_click(c, r, b)
            btn.grid(row=r,column=c)

def red_click(c, r, btn):
    print(btn.name, "clicked!")
    btn.configure(bg="red")

root = tk.Tk()
make_buttons()
root.mainloop()

对于许多对象,Python允许您从类本身外部在对象上创建“任意”属性。例如

class C:
    pass

>>> c = C()
>>> c.whatever
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: C instance has no attribute 'whatever'
>>> c.whatever = 'hi there'
>>> c.whatever
'hi there'
C类:
通过
>>>c=c()
>>>不管怎样
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:C实例没有属性“whater”
>>>c.whather='你好'
>>>不管怎样
“你好”

您可以将值绑定到
按钮
对象上的任意属性。然后可以使用属性查找检索该值

此外,最好使用
座位列表中保存的座位计划来创建按钮。您可以使用来帮助迭代这些值,同时仍然可以访问网格所需的索引:

import tkinter as tk

seats= [["A", "a1", "a2", "a3", "a4", "a5"],
        ["B", "b1", "b2", "b3", "b4", "b5"],
        ["C", "c1", "c2", "c3", "c4", "c5"],
        ["D", "d1", "d2", "d3", "d4", "d5"]]

def make_buttons():
    for r, row in enumerate(seats):
        for c, seat in enumerate(row[1:]):        # N.B. skip first element
            btn = tk.Button(root, text="Empty")
            btn.name = seat                       # arbitrary attribute on Button object
            btn['command'] = lambda c=c, r=r, b=btn:red_click(c, r, b)
            btn.grid(row=r,column=c)

def red_click(c, r, btn):
    print(btn.name, "clicked!")
    btn.configure(bg="red")

root = tk.Tk()
make_buttons()
root.mainloop()

对于许多对象,Python允许您从类本身外部在对象上创建“任意”属性。例如

class C:
    pass

>>> c = C()
>>> c.whatever
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: C instance has no attribute 'whatever'
>>> c.whatever = 'hi there'
>>> c.whatever
'hi there'
C类:
通过
>>>c=c()
>>>不管怎样
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:C实例没有属性“whater”
>>>c.whather='你好'
>>>不管怎样
“你好”

您可以按照传递行、列和按钮实例的相同方式传递文本。将文本发送到
红色\u单击
作为参数。如果它是按钮上的文本,那么您可以得到
btn['text']
BTW:分配给按钮的函数不能返回任何内容-它只能在屏幕上显示或为全局变量赋值。顺便说一句:您有一对
r,c
,所以您应该识别您单击的按钮。例如,
seats[r][c]
您可以按照传递行、列和按钮实例的相同方式传递文本。将文本发送到
red\u单击
作为参数。如果它是按钮上的文本,那么您可以得到
btn['text']
BTW:分配给按钮的函数不能返回任何内容-它只能在屏幕上显示或为全局变量赋值。顺便说一句:您有一对
r,c
,所以您应该识别您单击的按钮。例如,
seats[r][c]
你在两个方面帮助了我。1)它起作用了!2)你介绍我枚举,这是必要的。再次感谢。@johnenderson:没问题。@johnenderson:您可以在Python文档中阅读答案中提供的链接中的
enumerate
。@johnenderson:更新了答案,以更好地演示对对象上任意属性的赋值。您在两个方面帮助了我。1)它起作用了!2)你介绍我枚举,这是必要的。再次感谢。@johnenderson:没问题。@johnenderson:您可以在Python文档中阅读答案中提供的链接中的
enumerate
。@johnenderson:更新的答案可以更好地演示对对象上任意属性的赋值。