tkinter: Passing arguments when binding keys to functions

Tim Daneliuk tundra at tundraware.com
Thu Jan 9 16:20:06 EST 2003


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:

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

Is this general approach valid?

CommonHandler() in my case, happens to be an already existing routine
which is just called as CommonHandler(x, default=TRUE).  It, in turn,
calls os.path.abspath(x).  When this happens, I get the following
error:

       AttributeError: Event instance has no attribute '__getitem__'

This makes me believe that my general approach is correct, but something
about how I am calling the routine from the binding is different than
the normal call I make to CommonHandler.

One other question - is it possible to use the lambda form above to
pass more than a single argument?

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





More information about the Python-list mailing list