如何将参数传递给 tkinter 中的事件处理程序? [英] How to pass an argument to event handler in tkinter?
本文介绍了如何将参数传递给 tkinter 中的事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
widget.bind('<Button-1>',callback) # binding
def callback(self,event)
#do something
我需要向 callback()
传递一个参数.参数是一个字典对象.
I need to pass an argument to callback()
. The argument is a dictionary object.
推荐答案
您可以使用 lambda
定义一个匿名函数,如:
You can use lambda
to define an anonymous function, such as:
data={"one": 1, "two": 2}
widget.bind("<ButtonPress-1>", lambda event, arg=data: self.on_mouse_down(event, arg))
请注意,传入的 arg
只是一个普通的参数,您可以像所有其他参数一样使用:
Note that the arg
passed in becomes just a normal argument that you use just like all other arguments:
def on_mouse_down(self, event, arg):
print(arg)
这篇关于如何将参数传递给 tkinter 中的事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文