tkinter: Passing arguments when binding keys to functions
Tim Daneliuk
tundra at tundraware.com
Fri Jan 10 16:30:11 EST 2003
Abel Daniel wrote:
> Tim Daneliuk (tundra at tundraware.com) wrote:
> [...snipped...]
>
>>for x in range(len(array)):
>> y = array[x]
>> widget.bind('<F' + str(x+1) + '>', lambda arg=y : CommonHandler(arg))
>
> [...more snipped...]
>
>>So, I guess the heart of my question becomes, "Why does this formulation
>>pass the event object, not the array member to CommonHandler()>"
>
> Because the callback will get the event object as a parameter. (And that
> overwrites the default argument.)
>
> you need:
> widget.bind('<F' + str(x+1) + '>', lambda e, arg=y : CommonHandler(arg))
>
>
> abli
> abli at freemail.hu
>
Aha! That explains it. There is a minor error in your example, I believe.
Shouldn't the code be:
widget.bind('<F' + str(x+1) + '>', lambda e, arg=y : CommonHandler(e, arg))
^^^
--
------------------------------------------------------------------------------
Tim Daneliuk
tundra at tundraware.com
More information about the Python-list
mailing list