Python 检查用bind tkinter按下列表中的哪个按钮

Python 检查用bind tkinter按下列表中的哪个按钮,python,button,tkinter,bind,Python,Button,Tkinter,Bind,我有一个按钮列表,当我运行一个函数时,我需要检查列表中的哪个按钮被按下 import tkinter root = tkinter.Tk() def Function(event): print('The pressed button is:') listOfButtons = [] Button = tkinter.Button(root, text="Button 1") listOfButtons.append(Button) Button.pack() Button.bin

我有一个按钮列表,当我运行一个函数时,我需要检查列表中的哪个按钮被按下

import tkinter

root = tkinter.Tk()

def Function(event):
    print('The pressed button is:')

listOfButtons = []
Button = tkinter.Button(root, text="Button 1")
listOfButtons.append(Button)
Button.pack()
Button.bind("<Button-1>", Function)

Button = tkinter.Button(root, text="Button 2")
Button.pack()
listOfButtons.append(Button)
Button.bind("<Button-1>", Function)

root.mainloop()
导入tkinter
root=tkinter.Tk()
def功能(事件):
打印('按下的按钮是:')
按钮列表=[]
按钮=tkinter.Button(根,text=“按钮1”)
追加(按钮)
Button.pack()
Button.bind(“,函数)
按钮=tkinter.Button(根,text=“按钮2”)
Button.pack()
追加(按钮)
Button.bind(“,函数)
root.mainloop()
您可以使用

import tkinter

root = tkinter.Tk()

def Function(event):
    if event == 1:
        print('The pressed button is: 1')
    if event == 2:
        print('The pressed button is: 2')

listOfButtons = []
Button = tkinter.Button(root, text="Button 1", command= lambda: Function(1))
listOfButtons.append(Button)
Button.pack()

Button = tkinter.Button(root, text="Button 2",command= lambda: Function(2))
Button.pack()
listOfButtons.append(Button)

root.mainloop()
您可以使用这个命令

import tkinter

root = tkinter.Tk()

def Function(event):
    if event == 1:
        print('The pressed button is: 1')
    if event == 2:
        print('The pressed button is: 2')

listOfButtons = []
Button = tkinter.Button(root, text="Button 1", command= lambda: Function(1))
listOfButtons.append(Button)
Button.pack()

Button = tkinter.Button(root, text="Button 2",command= lambda: Function(2))
Button.pack()
listOfButtons.append(Button)

root.mainloop()

迭代列表中的所有按钮,并检查按钮是否为事件。小部件:

def Function(event):
    for button in listOfButtons:
        if button is event.widget:
            print(button['text'])
            return
正如@tobias_k所提到的,它过于复杂了。您已经有一个
按钮
作为
事件小部件
。所以解决方案很简单,比如打印(event.widget['text'])。但是,如果
函数
不仅可以通过单击按钮调用,还可以通过按钮/其他方式调用,这是必须检查的

在另一侧,不仅可以通过鼠标左键单击来按下按钮,因此
命令
选项更好

import tkinter

root = tkinter.Tk()

def Function(button):
    print(button['text'])


...
Button = tkinter.Button(root, text="Button 1")
Button.configure(command=lambda button=Button: Function(button))
...


Button = tkinter.Button(root, text="Button 2")
Button.configure(command=lambda button=Button: Function(button))
...

root.mainloop()

迭代列表中的所有按钮,并检查按钮是否为事件。小部件:

def Function(event):
    for button in listOfButtons:
        if button is event.widget:
            print(button['text'])
            return
正如@tobias_k所提到的,它过于复杂了。您已经有一个
按钮
作为
事件小部件
。所以解决方案很简单,比如打印(event.widget['text'])。但是,如果
函数
不仅可以通过单击按钮调用,还可以通过按钮/其他方式调用,这是必须检查的

在另一侧,不仅可以通过鼠标左键单击来按下按钮,因此
命令
选项更好

import tkinter

root = tkinter.Tk()

def Function(button):
    print(button['text'])


...
Button = tkinter.Button(root, text="Button 1")
Button.configure(command=lambda button=Button: Function(button))
...


Button = tkinter.Button(root, text="Button 2")
Button.configure(command=lambda button=Button: Function(button))
...

root.mainloop()

您不需要在按钮上使用
bind
,只需在创建按钮时使用其
命令
选项即可。不仅您不需要使用
bind
,而且您将按钮限制为仅当鼠标左键按下时才能运行
功能
。您不需要在按钮上使用
bind
,创建它们时,只需使用它们的
命令
选项。不仅不需要使用
绑定
,而且仅当使用鼠标左键按下按钮时,才可以将按钮限制为运行
函数
。您需要执行
命令=lambda:Function(1)
否则,您将在创建按钮时执行该函数,不是在单击时。你是对的,我更正了它。我认为有问题,在找到了答案,并看到了命令:DYou need do
command=lambda:Function(1)
否则,你在创建按钮时执行函数,而不是在单击时。你是对的,我纠正了它。我认为有问题,在找到了答案,并看到了命令:DBecause
print(event.widget['text'])
太简单了?@tobias_k这是一个很好的解决方案,同时也是一种奇怪的建议改进的方式。@tobias_k,是的,你是对的。我“过度思考”列表中的哪个按钮被按下了。因为
print(event.widget['text'])
太简单了?@tobias_k这是一个很好的解决方案,同时也是一种奇怪的建议改进的方式。@tobias_k,是的,你是对的。我“过度思考”了列表中的哪个按钮被按下了。