[tkinter] Event from menu event?

John Michelsen john.michelsen at gte.net
Tue May 25 13:10:31 EDT 1999


>----
>def myfunc(label):
>    print label
>
>mymenu.add_command(label = "foo", command = lambda: myfunc("foo"))
>----
>
>Which works, but does NOT work if I'm doing something like this:
>
>for each_class in draw_class_name:
>    mymenu.add_command(label = each_class, command = lambda:
>myfunc(each_class))
>
>I get this error:
>
>Exception in Tkinter callback
>Traceback (innermost last):
>  File "/usr/local/lib/python1.5/lib-tk/Tkinter.py", line 726, in __call__
>    return apply(self.func, args)
>  File "Haiku.py", line 167, in <lambda>
>    lambda: mondo_menu_call(each_class))
>NameError: each_class


How about:

for each_class in draw_class_name:
    mymenu.add_command(label = each_class, command = lambda v=each_class:
myfunc(v))

I came across the following lambda statement in PIL's thresholder.py:
im = self.image.point(lambda v,t=self.value: v>=t, "1")
and wondered why you couldn't simply put:
im = self.image.point(lambda v: v>=self.value, "1")
But you do need to put the default argument in the lambda statement.

John






More information about the Python-list mailing list