TkInter callback - how to ?

Timothy R Evans tre17 at cosc.canterbury.ac.nz
Wed Jun 23 19:39:05 EDT 1999


bernard <bh at cellware.de> writes:

> I have an array of buttons (a keypad) all using the same callback
> function - is there any way of finding out which button invoked
> the callback ? I suppose I am looking for a tcl/tk solution
> something like
>   -command "myCallback $thisButton"
> but tkInter only seems to provide
>   -command myCallback
> 
> Any help welcome
> 
> bernard

I posted a `Command' class that solves this a while ago, here it is
again:

class Command:
    def __init__(self, func, *args, **kw):
        self.func = func
        self.args = args
        self.kw = kw

    def __call__(self, *args, **kw):
        args = self.args + args
        kw.update(self.kw)
        apply(self.func, args, kw)

then do this:

command=Command(myCallback, thisbutton)

The class given is rather general, and I'm not quite sure if my
precedence for the keyword arguments makes sense.  As you won't use
the keyword arguments this won't matter.

--
Tim Evans





More information about the Python-list mailing list