"""" events (clicks of the left mouse button) on the button1 widget to the "self.button1Click" method. When button1 is left-clicked with the mouse, the self.button1Click() method will be invoked to handle the event. (3) Note that, although they aren't specified on the "bind" operation, self.button1Click() will be passed two arguments. The first, of course, will be "self", which is always the first argument to all class methods in Python. The second will be an event object. This technique of binding and event (that is, using the bind() method) always implicitly passes an event object as an argument. In Python/Tkinter, when an event occurs, it takes the form of an event object. An event object is extremely useful, because it carries with it all sorts of useful information and methods. You can examine the event object to find out what kind of event occurred, the widget where it occurred, and other useful bits of information. (4) So, what do we want to happen when button1 is clicked? Well, in this case we have it do something quite simple. It simply changes its color from green to yellow, and back again. (2) Let's make button2 (the "Goodbye!" button) actually do some useful work. We will make it shut down the window. So we bind a left-mouse click in button2 to the button2Click() method, and (6) We have the button2Click() method destroy the root window, the parent window of myapp. This will have a ripple effect, and will destroy all the children and descendents of the root. In short, every part of the GUI will be destroyed. Of course, to do this, myapp has to know who its parent is. So (7) we add code to its constructor to allow myapp to remember its parent. PROGRAM BEHAVIOR When you run this program, you will see two buttons. Clicking on the "OK" button will change its color. Clicking on the "Cancel" button will shut down the application. When our GUI is open, if you hit the TAB key on the keyboard, you will notice that the keyboard focus tabs between the two buttons. But if you hit the ENTER/RETURN key on the keyboard, nothing happens. That is because we have bound only mouse clicks, not keyboard events, to our buttons. Our next task will be to bind keyboard events to the buttons, also. Finally, notice that because the text of one button is shorter than the text of the other, the two buttons are of different sizes. This is rather ugly. We will fix that in a later program. [revised: 2002-10-01] >""" from Tkinter import * class MyApp: def __init__(self, parent): self.myParent = parent ### (7) remember my parent, the root 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.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) ### (2) def button1Click(self, event): ### (3) if self.button1["background"] == "green": ### (4) self.button1["background"] = "yellow" else: self.button1["background"] = "green" def button2Click(self, event): ### (5) self.myParent.destroy() ### (6) root = Tk() myapp = MyApp(root) root.mainloop()