tkinter: identifying callback button?

tre17 at student.canterbury.ac.nz tre17 at student.canterbury.ac.nz
Thu Sep 9 02:47:24 EDT 1999


Doug at news.vex.net (Lewis) writes:

> I have a bunch of (named) buttons which call the same callback.
> I've been trying to figure out how (or if) I can get the callback
> function to be able to identify which one.
> 
> For example....
> 
>     for x in range(0,5):
>         b = Button(frame,text='#%s' % x,command=self.xxx, name='b%s' % x)
>         b.pack()
> 
> So one of the five buttons gets pressed and calls App.xxx(self) ...
> but can App.xxx figure out which button triggered the command? 
> 
> I could save the button references in a list, but don't know if that
> would help.

The easiest way of doing this is the create a new class that inherits
from `Button'.

==============================
class MyButton(Button):
    def __init__(self, name, *args, **kw):
        self.name = name
        apply(Button.__init__, (self,)+args, kw)
        self.configure(command=self.clicked)

    def clicked(self):
        some_command(self.name)
==============================

--
Tim Evans




More information about the Python-list mailing list