Binding Widgets and Event Handlers


So far, the GUI programs you've seen don't do a whole lot. That's because there's no code associated with the activation of their widgets. Again, these widgets are like light fixtures that have been installed, but not connected to electrical wiring. Well, now it's time to get the electricity flowing; or in the case of GUI programming, it's time to write event handlers and bind them with events.

Introducing the Click Counter Program

The Click Counter program has a button that does something: it displays the number of times the user has clicked it. Technically, the button's event handler takes care of updating the click count and changing the text on the button. Figure 10.10 shows off the program.


Figure 10.10: The button's event handler updates the number of times the button is clicked.

Setting Up the Program

As my traditional first step, I import the GUI module:

 # Click Counter # Demonstrates binding an event with an event handler # Michael Dawson - 6/6/03 from Tkinter import * 

Next, I start the Application class definition:

 class Application(Frame):     """ GUI application which counts button clicks. """     def __init__(self, master):         """ Initialize the frame. """         Frame.__init__(self, master)         self.grid()         self.bttn_clicks = 0    # the number of button clicks         self.create_widget() 

You've seen most of this code before. The new line is self.bttn_clicks = 0, which creates an object attribute to keep track of the number of times the user clicks the button.

Binding the Event Handler

In the create_widget() method, I create a single button:

     def create_widget(self):         """ Create button which displays number of clicks. """         self.bttn = Button(self)         self.bttn["text"]= "Total Clicks: 0"         self.bttn["command"] = self.update_count         self.bttn.grid() 

I set the Button widget's command option to the update_count() method. As a result, when the user clicks the button, the method gets invoked. Technically, what I've done is bind an event (the clicking of Button widget) to an event handler (the update_count() method).

In general, you set a widget's command option to bind the activation of the widget with an event handler.

Creating the Event Handler

Next, I write the update_count() method, which handles the event of the button being clicked:

     def update_count(self):         """ Increase click count and display new total. """         self.bttn_clicks += 1         self.bttn["text"] = "Total Clicks: "+ str(self.bttn_clicks) 

This method increments the total number of button clicks and then changes the text of the button to reflect the new total. That's all it takes to get a button to do something useful (or almost useful).

Wrapping Up the Program

The main part of the code should be pretty familiar to you by now:

 # main root = Tk() root.title("Click Counter") root.geometry("200x50") app = Application(root) root.mainloop() 

I create a root window and set its title and dimensions. Then I instantiate a new Application object with the root window as its master. Lastly, I start up the root window's event loop to bring the GUI to life on the screen.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net