Event Binding and Variable Passing

Bruce Wolk bawolk at pacbell.net
Sun Jun 25 17:32:10 EDT 2000


You need to use a lamda expression.

def fun(x):
	print x
b.bind('<Button-1>', lambda event, b=x: fun(b))

The event argument is ignored here.

Happy TKinter programming!

Bruce


kstar18 at hotmail.com wrote:
> 
> hi,
> 
> I'm relatively new to python and am experimenting with some Tk binding.
> I was wondering how I would pass a variable to a function that is called
> when a Tk event occurs.
> 
> For example,
> b = Button(root, text = 'hi')
> b.pack()
> x = 5
> b.bind('<Button-1>', self.fun(x))
> 
> def fun(self, event, x):
>    print x
> 
> This doesn't seem to work, and the compiler gives me complaints about
> 'self' and the variable being passed.
> 
> Any help would be greatly appreciated.
> 
> Thanks!
> 
> Kapil
> 
> This method doesn't seem to work.
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.



More information about the Python-list mailing list