我有几个小部件绑定到同一个函数调用。如何判断哪一个触发了该呼叫?
答案 0 :(得分:6)
该事件有一个小部件字段,可帮助您区分哪个小部件是源:
from Tkinter import *
class MyObj:
def callback(self, event):
print event.widget.message
obj = MyObj()
root = Tk()
btn=Button(root, text="Click")
btn.bind('<Button-1>', obj.callback)
btn.pack()
btn.message = 'Hello'
btn2=Button(root, text="Click too")
btn2.bind('<Button-1>', obj.callback)
btn2.message = 'Salut'
btn2.pack()
root.mainloop()