Two naive Tkinter questions
Alan Gauld
alan.gauld at btinternet.com
Mon Nov 3 03:15:28 EST 2003
On Sun, 02 Nov 2003 21:32:12 GMT, "Andrew Koenig" <ark at acm.org>
wrote:
> 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):
widget['bg']='blue'
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!
HTH,
Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Python-list
mailing list