Event Binding and Variable Passing

richard_chamberlain richard_chamberlain at ntlworld.com
Sat Jun 24 09:37:39 EDT 2000


Hi Kapil,

from Tkinter import *
root=Tk()
b = Button(root, text = 'hi')
b.pack()
x = 5
b.bind('<Button-1>', lambda event: fun(event,x))

def fun(event, x):
    print x

root.mainloop()

You should look at lambda to pass the variables you want.

Actually the code you have written you needn't pass x at all because fun
would have access to it anyway - is this part of a class? if not you don't
need the self you used in your example, if it is you'll need to fiddle with
my example a bit.

Richard
<kstar18 at hotmail.com> wrote in message news:8j0g88$htm$1 at nnrp1.deja.com...
> 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