Hi friends.,
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.
thanks in advance
sag
Hi friends.,
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.
thanks in advance
sag
What about
import functools
def callback(self, event, param):
pass
arg = 123
widget.bind("", functools.partial(callback, param=arg))
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
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.OnMouseDown(event, arg))
Edit in response to a question in the comments:
The arg passed in becomes just a normal argument that you use just like all other arguments:
def OnMouseDown(self, event, arg):
print arg