如何传递Tkinter的一个参数的事件处理程序? [英] How to pass an argument to event handler in tkinter?
本文介绍了如何传递Tkinter的一个参数的事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
widget.bind('<Button-1>',callback) # binding
def callback(self,event)
#do something
我需要一个参数传递给回调()
。该参数是一个字典对象。
I need to pass an argument to callback()
. The argument is a dictionary object.
推荐答案
您可以使用<一个href=\"https://docs.python.org/3/tutorial/controlflow.html#lambda-ex$p$pssions\"><$c$c>lambda$c$c>定义一个匿名功能,如:
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屋!
查看全文