Two naive Tkinter questions

Pierre Quentel quentel.pierre at wanadoo.fr
Mon Nov 3 11:00:14 EST 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 
by:
self.b1=Button(self, bg="red")
self.b1.bind("<Button-1>",self.setcolor)

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):
    event.widget["bg"]="blue"

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:
       event.widget["bg"]="blue"
   elif event.widget is self.b2:
       event.widget["bg"]="green"







More information about the Python-list mailing list