simple button class

Martin Franklin MFranklin1 at gatwick.westerngeco.slb.com
Fri May 30 04:27:33 EDT 2003


Mark Light wrote:
> I am using Tkinter
> 
> 
> "Mark Light" <light at soton.ac.uk> wrote in message
> news:bb5bio$8c8$1 at aspen.sucs.soton.ac.uk...
> 
>>Hi,
>>      I am trying to write a small gui program that has a 24 buttons that
>>are essentially the same. I would like to do this via a class and call an
>>instance for each button. I have read and re-read the class stuff and get
> 
> a
> 
>>bit confused - could some one start me off. Initially I just want a class
>>where each instance would generate a button that when pressed would print
> 
> to
> 
>>stdout I am button 1 I am button 2 .. etc - hopefully I will be able to
> 
> pick
> 
>>things up from there.
>>
>>Many thanks,
>>
>>Mark.
>>

Mark,

Here goes.... untested...

from Tkinter import *

class Callback:
     def __init__(self, button):
         # called when button is created
         self.button = button

     def __call__(self):
         # called when button is pushed
         print "I am button", self.button

root=Tk()
for a in range(1):
     b=Button(root, text=a, command=Callback(a))
     b.pack()
root.mainloop()



HTH
Martin




















More information about the Python-list mailing list