[Pythonmac-SIG] problems with Tkinter on 1.6a2

Bob Savage savageb@pacbell.net
Tue, 06 Jun 2000 22:49:32 -0700


on 6/6/00 3:47 PM, Lewis, Cameron wrote:

> Question:
> Like to make buttons very much so:
> 
> for b in "many things"
> make many, many, many buttons(callback)
> 
> Note: Not real code, please not try and run.
> When button say "Oh joy I have been pushed"
> How do I know which button???
> Not type well so not like many, many, many, many callback.
> Like one callback that say "Hello from button Larry"???
> Solution not seem obvious to me (like many other things ;-) ).

Sounds like what you want to do is subclass Button:

#################################################################
# probably won't work as I'm doing this off the
# top of my head, but you get the picture

from Tkinter import *


class b_Button(Button):
    def __init__(self, r, myName):
        self.myName = myName
        Button.__init__(self, r, text=myName, command=self.introduceMyself)
    def introduceMyself(self):
        print "Hello, from button", self.myName

root = Frame()
root.pack()

for b in ("shoes", "ships", "sealing-wax", "cabbages", "kings"):
    b_Button( root, b).pack()
    
root.mainloop()