[Tkinter-discuss] Convert Tk command string to Python callback

Michael Lange klappnase at web.de
Thu Nov 17 16:47:11 CET 2005

On Wed, 16 Nov 2005 11:45:17 -0800
"Russell E. Owen" <rowen at cesmail.net> wrote:

> In article <20051114215954.07accbed.klappnase at web.de>,
>  Michael Lange <klappnase at web.de> wrote:
> > ...I just wondered if there is a way to ...access the associated python callback from the tk command 
> > string...
> I hope this helps. I've appended code that registers python functions to 
> be called from tcl/tk. This is from my RO python package 
> <http://www.astro.washington.edu/rowen/>, from RO.TkUtil.

Thanks Russell,

In the meantime I jfound another way to achieve what I want; I store the python comands in a _callbacks dictionary with
the tk command strings as keys. I had to write a hacked version of _bind() for that, but I had to
do so anyway. After the line

     cmd = ('if {"[%s %s]" == "break"} break\n' % (funcid, self._subst_format_str_dnd))

in _bind() I added

     self._callbacks[cmd] = func

so I can do now something like:

    def deliver_event(self, event, sequence):
        res = 'break'
        tclCmd = self.bind(sequence)
             callback = self._callbacks[tclCmd]
             res = callback(event)
        except KeyError:
        return res

Then I can write handlers for several events like this:

    def deliver_sequence1(self, event):
        return self.deliver_event(event, sequence1)

This seems to work pretty well.



More information about the Tkinter-discuss mailing list