Lambdas and variables

John Fouhy jfouhy at paradise.net.nz
Tue Jul 27 19:35:39 EDT 2004


So I'm trying to generate Tkinter callback functions on the fly, but
it's not working, and I don't understand what's going on.

Here is an example program:

--------------------------
from Tkinter import *

def printSomething(x):
    print x

tk = Tk()
stuff = ['foo', 'bar', 'baz', 'zif', 'zaf', 'zof']

for x in stuff:
    l = Label(tk, text=x)
    l.pack()
    l.bind('<Enter>', lambda e: printSomething(x))

tk.mainloop()
--------------------------

The desired behaviour is that the program should print to stdout the
text on the label whenever the mouse enters that widget.

However, what actually happens is that it prints out 'zof' whenever
the mouse enters any of the widgets.

To further confuse me, if I add a bit of indirection:

--------------------------
from Tkinter import *

def makeFunction(x):
    return lambda e: printSomething(x)

def printSomething(x):
    print x

tk = Tk()
stuff = ['foo', 'bar', 'baz', 'zif', 'zaf', 'zof']

for x in stuff:
    l = Lable(tk, text=x)
    l.pack()
    l.bind('<Enter>', makeFunction(x))

tk.mainloop()
--------------------------

...then everything works as it should.

I am running Python 2.3.4.

Can anyone explain this to me?

-- 
John.



More information about the Python-list mailing list