tkinter: Passing arguments when binding keys to functions

Tim Daneliuk tundra at tundraware.com
Fri Jan 10 18:30:07 CET 2003


Martin Franklin wrote:
> On Thu, 2003-01-09 at 21:20, Tim Daneliuk wrote:
> 
>>I have an array of 12 items that I want to associate with the 12 Function
>>keys.  I want to call a common handler function for all 12 keys and
>>initialize the bindings to pass an argument to the handler so it
>>can tell who invoked it.  I've tried something like this, but to no
>>avail:
<SNIP>

> 
> 
> Not sure I can answer your exact questions but here is another way 
> to do what (I think...) you want.
> 

<SNIP>

Hmm, your way does work, up to a point.  For some reason, if I run
this code and press, say, F10 first (which should do nothing), the
program produces no further output even when pressing one of the
bound keys.

However, I have played with this a bit more and can now ask a more
specific question.  It concerns my original formulation of the
binding:

for x in range(len(array)):
     y = array[x]	
     widget.bind('<F' + str(x+1) + '>', lambda arg=y : CommonHandler(arg))

In playing with this, I've come to realize that what is happening
is that CommonHandler is being called properly at the time a
function key is pressed, however, *the Function keypress event is
being passed to it, NOT the value of y*.  This is what's confusing
CommonHandler because it is expecting a string, not an event object.

So, I guess the heart of my question becomes, "Why does this formulation
pass the event object, not the array member to CommonHandler()>"

(Clearly, I have a lot to learn about lambda constructions ...)


-- 
------------------------------------------------------------------------------
Tim Daneliuk
tundra at tundraware.com





More information about the Python-list mailing list