tkinter: Passing arguments when binding keys to functions

Abel Daniel abli at freemail.hu
Fri Jan 10 18:39:44 EST 2003


Tim Daneliuk (tundra at tundraware.com) wrote:
> 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))
Depends.
If you need the event object (i.e. the data it contains, like
event.widget, etc.) then you need to pass it in as you wrote.
However, it is not mandatory. If CommonHandler looks like:
def Commonhandler(arg):
    print arg
    # or popup a modal dialog asking wheter the user wants to quit
    # i.e. doing something for which event data is not needed

then its not needed.
(I assumed you didnt want to change CommonHandler : ) )

abli





More information about the Python-list mailing list