Event Binding and Variable Passing 
Author Message
 Event Binding and Variable Passing

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.*-*-*.com/
Before you buy.



Tue, 10 Dec 2002 03:00:00 GMT  
 Event Binding and Variable Passing
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

Quote:

> 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.



Wed, 11 Dec 2002 03:00:00 GMT  
 Event Binding and Variable Passing
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

Quote:

> 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.



Thu, 12 Dec 2002 03:00:00 GMT  
 Event Binding and Variable Passing


Quote:
>You need to use a lamda expression.

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

                        .
                        .
                        .
Can one of the old(er?)-timers help me with this?
There's a sect (claiming the Guido among its sym-
pathizers) that deprecates lambdas for Python, no?
But their reasons (very abstractly, that object-
oriented metaprogramming gives more than enough
goodies, making lambdas essentially redundant) do
*not* apply for communication with the event- and
binding-mechanisms adapted from Tcl, true?  The
conclusion would be, then, that Tkinter forces a
fundamental need for lambdas.
--


Business:  http://www.Phaseit.net
Personal:  http://starbase.neosoft.com/~claird/home.html



Thu, 12 Dec 2002 03:00:00 GMT  
 Event Binding and Variable Passing
[posted & mailed]

Cameron subtly baited:


    >You need to use a lamda expression.
    >
    >def fun(x):
    > print x
    >b.bind('<Button-1>', lambda event, b=x: fun(b))
    .
    .
    .
    Can one of the old(er?)-timers help me with this?
    There's a sect (claiming the Guido among its sym-
    pathizers) that deprecates lambdas for Python, no?
    But their reasons (very abstractly, that object-
    oriented metaprogramming gives more than enough
    goodies, making lambdas essentially redundant) do
    *not* apply for communication with the event- and
    binding-mechanisms adapted from Tcl, true? The
    conclusion would be, then, that Tkinter forces a
    fundamental need for lambdas.

Well, my version of IDLE contains all of 9 lambdas, only 3 of which
are used with Tkinter. So obviously Guido is no big fan of lambda,
but neither does he abstain from its use.

However, the fact that that much Tkinter code uses only 3 lambdas
belies your assertion.

Of course there are a multiplicity of sects within Python, sharing little
more than a common shortage of {*filter*}s...

- Gordon



Wed, 18 Dec 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. event bindings which pass the event himself first..?

2. Passing user defined variables to mouseclick/enter/leave events

3. Passing user defined variables to mouseclick/enter/leave events

4. Bind variables to pass to Microsoft Word Application.Wordbasic interface

5. pass different variables into bind..??

6. passing variable to bindings??

7. Character variable variable passed as a subroutine argument

8. Pass variable to variable by reference?

9. Passing DLL events to parent frame?

10. Passing DLL events to parent frame

11. Pass data between subVIS using Event structures.

12. GUI/event loop programming is Continuation Passing - paper

 

 
Powered by phpBB® Forum Software