[Tutor] how to call a binding method from an imported module

Kent Johnson kent37 at tds.net
Fri Oct 31 22:46:53 CET 2008


On Fri, Oct 31, 2008 at 4:16 PM,  <dwbarne at earthlink.net> wrote:
> This problem involves a callback method while using 'bind'. The bind statement and the callback function are both in a module imported to the main program. Relevant code snippets are as follows:
>
> #++++ begin snippet
>
> # main code
> <code>
> import module_Editor
> .
> class MyClass():
> <code>
>    def editor(self):
>
>        module_Editor.my_Editor(self,self.frameParent)
> <code>
> # end of main code
>
> # module 'module_Editor'
> <imports>
> def my_Editor(self,parentFrame):

This is confusing. Is my_Editor() part of a class? If not, why does it
take a self parameter?
> <code>
>    self.textMyCode.bind(
>        "<KeyPress-Return>",
>        handlerTextLineNumbersReturn(self)

Presumably you are using Tkinter?

Note that you are calling handlerTextLineNumbersReturn() here. I would
expect a TypeError here because you only pass one argument.

Normally the second argument to bind() is a function, not a call to a function.
>        )

> <code>
> def handlerTextLineNumbersReturn(self,event):
>    def temp():
>
>        print '\n** In handlerTextLineNumbersReturn'
>
>    return temp

I don't understand why you define and return another function here.
Normally an event handler does not return a value.

> <code>
> # end of module 'module_Editor'
>
> # ++++ end snippet
>
> When the bind callback handler is called, the following error is returned:
>
> ++++ begin error
> Exception in Tkinter callback
> Traceback (most recent call last):
>  File "c:\Python251_102507\lib\lib-tk\Tkinter.py", line 1403, in __call__
>    return self.func(*args)
> TypeError: tempDef() takes no arguments (1 given)

What is tempDef() ?
>
> ++++ end error
>
> The above approach works for widgets in the module calling callback handlers that return a def like the above, but bind statements apparently do not like this approach for some reason.

Can you give an example of what you are doing that works?

> Any ideas on what I'm doing wrong here? Maybe a "*args" needs to go somewhere in the calling or def statements? If so, where does it go?

The code seems a bit confused. I don't understand what you are trying
to do. Possibly what you want is this:

def my_Editor(self,parentFrame):
<code>
   self.textMyCode.bind(
       "<KeyPress-Return>",
       handlerTextLineNumbersReturn
       )
<code>
def handlerTextLineNumbersReturn(event):
       print '\n** In handlerTextLineNumbersReturn'

Kent


More information about the Tutor mailing list