Two naive Tkinter questions

Alan Gauld alan.gauld at
Mon Nov 3 09:15:28 CET 2003

On Sun, 02 Nov 2003 21:32:12 GMT, "Andrew Koenig" <ark at>
> Your first suggestion, knowing that setcolor deals with self.b1, doesn't
> work with my application because I'm going to have lots of these buttons,
> and I want to be able to set their colors independently.

A slight variant on the technique using lambda is: 

   def setcolor(self,widget=self):
   def createWidgets(self):
       self.b1 = Button(self, bg = "red", 
                        command=lambda: self.setcolor(self.b1))

Here we use the lambda to call the setcolor method with 
the widget parameter and use that within the setcolor method.

This way you keep one method but call it from several places. 
The downside is you introduce an extra function call, but in 
a GUI event handler that's not going to be a problem!


Alan G.


Author of the Learn to Program website

More information about the Python-list mailing list