[Tutor] Multiple buttons, One callback

David Dashifen Kees dashifen at dashifen.com
Fri Sep 16 23:42:15 CEST 2005


Greetings all:

First off, the necessary statistics:  Python 2.4 using Tkinter as a GUI 
manager.

Now, the problem:  I've got an interface that generates a number of 
buttons.  Each of these buttons performs the same basic task over 
different data based on which button is clicked.  In other languages, 
I've always been about to bind each button to one callback function 
which could then determine which button was clicked, and from that data 
grab the necessary data and perfrom the operations on that data.

So, in python this is what I did.  I made a class that inherits from the 
Tkinter.Toplevel class.  This class dynamically creates its interface 
which contains a series of rows.  On each row the following widgets 
appear in the following order: button, label, label, button button.  
Now, each of those buttons takes the information in the first label 
(maintained as a StringVar) and performs a similar operation upon it.  
I've been able to bind each button to the same callback function (that 
part's trivial) but what hasn't been trival is determining which button 
was clicked and, from this information, gathering the correct datum from 
the list of StringVars which make up the first label text.

In other languages, I've been able to set an id on the buttons that the 
callback function could access and that id would be the index within 
StringVar array to gather the data as well as a code to determine which 
button was clicked.  Thus, the buttons for one row could have the 
following ids:  0|0, 0|1, 0|2.  This would refer to take the datum 
string_vars[0] and perform operation 0, 1, or 2 upon it.  However, I 
cannot figure out how to create this behavior in python to date.

So, what do you think I need to do?  Is there an easier way?

Thanks all,
Dash Kees


More information about the Tutor mailing list