Creating a GUI Using a Class


As you've learned in other chapters, organizing your code into classes can make your programming life a lot easier. It's often beneficial to write larger GUI programs by defining your own classes. So next, I show you how to write a GUI program by organizing the code with a class.

Introducing the Lazy Buttons 2 Program

The Lazy Buttons 2 program is simply the Lazy Buttons program rewritten using a class. The program appears exactly the same to the user, but behind the scenes I've done some restructuring. Figure 10.9 shows the ever-so familiar program in action.


Figure 10.9: It's d j vu all over again. The program looks the same as its predecessor even though there are significant changes under the hood.

Importing the Tkinter Module

Though there are significant structural changes to the program, importing the GUI module is still the same:

 # Lazy Buttons 2 # Demonstrates using a class with Tkinter # Michael Dawson - 6/5/03 from Tkinter import * 

Defining the Application Class

Next, I create a new class, Application, based on Frame:

 class Application(Frame):     """ A GUI application with three buttons. """ 

Instead of instantiating a Frame object, I'll end up instantiating an Application object to hold all of the buttons. This works since an Application object is just a specialized type of Frame object.

Defining a Constructor Method

Next, I define Application's constructor:

    def __init__(self, master):        """ Initialize the Frame. """        Frame.__init__(self, master)        self.grid()        self.create_widgets() 

Since an Application object is just a specialized kind of Frame object, I initialize it through Frame's constructor. I pass along the Application object's master, so it gets properly set as the master. Finally, I invoke the Application object's create_widgets() method, which I define next.

Defining a Method to Create the Widgets

I define a method that creates all three buttons, create_widgets():

    def create_widgets(self):        """ Create three buttons that do nothing. """        # create first button        self.bttn1 = Button(self, text = "I do nothing!")        self.bttn1.grid()        # create second button        self.bttn2 = Button(self)        self.bttn2.grid()        self.bttn2.configure(text = "Me too!")        # create third button        self.bttn3 = Button(self)        self.bttn3.grid()        self.bttn3["text"] = "Same here!" 

The code looks pretty similar to the code that creates the buttons in the original Lazy Buttons program. An important difference is that bttn1, bttn2, and bttn3 are attributes of the Application object. Another important difference is that I use self as the master for the buttons so that the Application object is their master.

Creating the Application Object

In the main section of code, I create a root window and give it a title and a proper size:

 # main root = Tk() root.title("Lazy Buttons 2") root.geometry("200x85") 

Then, I instantiate an Application object with the root window as its master:

 app = Application(root) 

This code creates a Application object with the root window as its master. The Application object's constructor invokes the object's create_widgets() method. This method then creates the three buttons, with the Application object as their master.

Finally, I invoke the root window's event loop to kick off the GUI and keep it running:

 root.mainloop() 




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