Python 如何将参数传递给 tkinter 中的事件处理程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3296893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass an argument to event handler in tkinter?
提问by sag
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.
我需要将参数传递给callback(). 参数是一个字典对象。
回答by Philipp
What about
关于什么
import functools
def callback(self, event, param):
pass
arg = 123
widget.bind("", functools.partial(callback, param=arg))
回答by luc
I think that in most cases you don't need any argument to a callback because the callback can be an instance method which can access the instance members:
我认为在大多数情况下,回调不需要任何参数,因为回调可以是可以访问实例成员的实例方法:
from Tkinter import *
class MyObj:
def __init__(self, arg):
self.arg = arg
def callback(self, event):
print self.arg
obj = MyObj('I am Obj')
root = Tk()
btn=Button(root, text="Click")
btn.bind('<Button-1>', obj.callback)
btn.pack()
root.mainloop()
But I think the functools solution proposed by Philipp is also very nice
不过我觉得Philipp提出的functools方案也很不错
回答by Bryan Oakley
You can use lambdato define an anonymous function, such as:
您可以使用lambda来定义匿名函数,例如:
data={"one": 1, "two": 2}
widget.bind("<ButtonPress-1>", lambda event, arg=data: self.on_mouse_down(event, arg))
Note that the argpassed in becomes just a normal argument that you use just like all other arguments:
请注意,arg传入的只是一个普通参数,您可以像所有其他参数一样使用它:
def on_mouse_down(self, event, arg):
print(arg)
回答by Iordanov K.
Pass the callback function to the instance and call it from the instance method.
将回调函数传递给实例并从实例方法中调用它。
from tkinter import *
class MyClass:
def __init__(self, my_callback, message):
self.my_callback = my_callback
self.message = message
def callback(self, event):
self.my_callback(self)
def my_callback(o):
print(o.message)
obj = MyClass(my_callback, "I am instance of MyClass")
root = Tk()
btn=Button(root, text="Click")
btn.bind('<Button-1>', obj.callback)
btn.pack()
回答by Gabriel Staples
Here's the simplest and easiest-to-read solution of them all I think:
这是我认为最简单、最容易阅读的解决方案:
widget.bind('<Button-1>', callback2)
def callback(self, event, custom_arg=None): #change "None" to whatever you want the default value to be
#do something
def callback2(self, event):
callback(event, custom_arg=something_you_set) #set custom_arg to whatever you want it to be when Button-1 is pressed
回答by Serag Hassouna
You can also supply arguments to a callback function of a widget, given only that this widget is defined as a part of a class definition,, i.e. consider this tiny python 2.7 program (without the parts responsible of program's execution):
您还可以为小部件的回调函数提供参数,前提是该小部件被定义为类定义的一部分,即考虑这个微型 python 2.7 程序(没有负责程序执行的部分):
import Tkinter as tk #To be able to get "tk.Button" safely
from Tkinter import *
class EXAMPLE(Frame):
def __init__(self,master=None):
Frame.__init__(self,master)
#make the widgets appear to a grid of size = 2 X 2
for row in range(2):
self.grid_rowconfigure(row,minsize=20)
for col in range(2):
self.grid_columnconfigure(col,minsize=20)
#Call our METHOD OF INTEREST
self.AnyMethod()
#This is our method of interest
def AnyMethod(self):
#arguments to be supplied
self.arg1 = 'I am 1st argument'
self.arg2 = 'I am 2nd argument'
self.arg3 = 'I am 3rd argument'
#Draw the widget, & supply its callback method
self.widgetname=tk.Button(self.master,text="My Button",command=self.method_callback)
self.widgetname.grid(row=0,column=0)
#create a so-called 'shell method' to swallow the REAL callback function
def method_callback(self):
func_callback(self.arg1,self.arg2,self.arg3)
#Define the REAL callback function in the Module's scope
def func_callback(arg1,arg2,arg3):
print arg1
print arg2
print arg3
NOTE THATthe supplied arguments must be proceeded with self.
请注意,必须继续提供提供的参数self.

