【发布时间】:2024-04-23 12:40:02
【问题描述】:
我有一个按钮列表,当我运行一个函数时,我需要检查该列表中的哪个按钮被按下。
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()
【问题讨论】:
-
您不需要在按钮上使用
bind,只需在创建按钮时使用它们的command选项。 -
不仅你不需要使用
bind,而且你限制你的按钮能够运行Function,当且仅当它被按下鼠标左键时。
标签: python button tkinter bind