Two naive Tkinter questions

Pierre Quentel quentel.pierre at
Mon Nov 3 17:00:14 CET 2003

When you define self.b1, instead of using "command=self.setcolor"  
you can bind the event "click with left button" to a callback method 
self.b1=Button(self, bg="red")

With this syntax, setcolor must be defined with an argument, which 
is an instance of the class Event. This instance has a "widget" 
attribute, so with event.widget you are sure to get the widget which 
called the method. You define setcolor by :

def setcolor(self,event):

You can try to bind the other button to setcolor the same way. Then 
if you want a different color for each button, define setcolor by :

def setcolor(self,event):
   if event.widget is self.b1:
   elif event.widget is self.b2:

More information about the Python-list mailing list