Returning values from a lambda

William Tanksley wtanksle at
Fri Nov 16 02:56:50 CET 2001

On Fri, 16 Nov 2001 10:36:06 +1000 (EST), John McMonagle wrote:
>I wish to return values from a lambda function which is bound to the
>Tkinter button command option.  For example,

Wait a second.  You don't want to return values; you want to modify
variables.  There's a HUGE difference; lambdas can do the first, but they
don't support any of the syntax to do the second.

The solution is to not use a lambda.  Just define a function which
modifies the variables you need, and pass that function's name in place of
the lambda.

>def operations(a,b):
>  return a+b, a*b

def operations(a,b):
   x = a+b
   y = a*b

>from Tkinter import *
>root = Tk()
>btn = Button(root, text='Press Me', command=lambda a=2, b=5: operations(a,b))

With the function I've defined, things should work.


-William "Billy" Tanksley

More information about the Python-list mailing list