[Tutor] binding problem

Alan Gauld alan.gauld at btinternet.com
Thu Nov 3 19:22:29 CET 2011


On 03/11/11 13:01, Chris Hare wrote:
> Thanks for the advice.  When I do that, I get this error
>
> Exception in Tkinter callback
> Traceback (most recent call last):
>    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
>      return self.func(*args)
> TypeError: focus_set() takes exactly 1 argument (2 given)
>
> In situations like this where the function isn't one you wrote, how to you debug these?

Its not a bug in the function, its a bug in your use of bind().

bind() assumes that the function you are binding takes two arguments: 
self and the event. This is different to the function assigned to a 
command parameter which only takes a single (self) argument.

So if you want to use a built-in function that doesn't match what bind 
expects (either more or less arguments) you need to provide a wrapper. 
Usually this will be in the form of a lambda:

Where the function is short of args:

self.list.bind("<Button-1>",
                 lambda e: the_builtin() )  # drop the event

Where the function needs extra args:

self.list.bind("<Button-1>",
                 lambda e: aBuiltIn(e, def1, def2) ) )  # supply defaults


However if you are the creator of the method - as seems to be true here 
- you just change the signature of your function/method to take two args 
(and possibly ignore the event).

def myMethod(self, e=None)  # just ignore e if you don't need it
     # ...

self.list.bind("<Button-1>", self.myMethod )


HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list