"""") rather than a mouse event. We want a press of the RETURN key on the keyboard and a click of the left mouse button to have the same effect on the widget, so we bind the same event handler to both types of events. This program shows that you can bind multiple types of events to a single widget (such as a button). And you can bind multiple combinations to the same event handler. (3) (4) Now that our button widgets respond to multiple kinds of events, we can demonstrate how to retrieve information from an event object. What we will do is to pass the event objects to (5) a "report_event" routine that will (6) print out information about the event that it obtains from the event's attributes. Note that in order to see this information printed out on the console, you must run this program using python (not pythonw) from a console window. PROGRAM BEHAVIOR When you run this program, you will see two buttons. Clicking on the left button, or pressing the RETURN key when the button has the keyboard focus, will change its color. Clicking on the right button, or pressing the RETURN key when the button has the keyboard focus, will shut down the application. For any of these keyboard or mouse events, you should see a printed message giving the time of the event and describing the event. [Revised: 2002-09-26] >""" from Tkinter import * class MyApp: def __init__(self, parent): self.myParent = parent self.myContainer1 = Frame(parent) self.myContainer1.pack() self.button1 = Button(self.myContainer1) self.button1.configure(text="OK", background= "green") self.button1.pack(side=LEFT) self.button1.focus_force() ### (0) self.button1.bind("", self.button1Click) self.button1.bind("", self.button1Click) ### (1) self.button2 = Button(self.myContainer1) self.button2.configure(text="Cancel", background="red") self.button2.pack(side=RIGHT) self.button2.bind("", self.button2Click) self.button2.bind("", self.button2Click) ### (2) def button1Click(self, event): report_event(event) ### (3) if self.button1["background"] == "green": self.button1["background"] = "yellow" else: self.button1["background"] = "green" def button2Click(self, event): report_event(event) ### (4) self.myParent.destroy() def report_event(event): ### (5) """Print a description of an event, based on its attributes. """ event_name = {"2": "KeyPress", "4": "ButtonPress"} print "Time:", str(event.time) ### (6) print "EventType=" + str(event.type), \ event_name[str(event.type)],\ "EventWidgetId=" + str(event.widget), \ "EventKeySymbol=" + str(event.keysym) root = Tk() myapp = MyApp(root) root.mainloop()