Using a Root Window


The foundation of your GUI program is its root window, upon which you add all other GUI elements. If you think of your GUI as a tree, then the root window is, well, the root. Your tree can branch out in all directions, but every part of it is, directly or indirectly, anchored by the root.

Introducing the Simple GUI Program

The Simple GUI program creates about the simplest GUI possible: a single window. Figure 10.5 shows the results of the program.


Figure 10.5: The program creates only a lone window. Hey, you have to start somewhere.

TRAP

Running a Tkinter program directly from IDLE will cause either your program or IDLE to lock up. The problem stems from the fact that IDLE is written using the Tkinter toolkit too. The simplest solution is to run your Tkinter program directly. In Windows, you can do this simply by double-clicking your program's icon.

In addition to the window pictured in Figure 10.5, Simple GUI may generate another window (depending upon your operating system): the familiar console window, pictured in Figure 10.6.

click to expand
Figure 10.6: A GUI program can generate a console window too.

Although you may think that this console window is just an eyesore, marring your otherwise perfect GUI, don't be so quick to dismiss it. The console window can provide valuable feedback if (and when) your Tkinter program produces errors. Also, don't close the console window, because that will close your GUI program along with it.

TRICK

Once you get your GUI programming running perfectly, you may want to suppress its accompanying console window. On a Windows machine, the easiest way to do this is to change the extension of your program from py to pyw.

Importing the Tkinter Module

Finally, it's time to get your hands dirty with some code! The first thing I do in the Simple GUI program is import the Tkinter module:

 # Simple GUI # Demonstrates creating a window # Michael Dawson - 6/5/03 from Tkinter import * 

The previous code imports all of Tkinter directly into the program's global name-space. Normally, you want to avoid doing something like this; however, a few modules, like Tkinter, are designed to be imported in this way. You'll see just how this helps in the next line of code.

Creating a Root Window

To create a root window, I instantiate an object of the Tkinter class Tk:

 # create the root window root = Tk() 

Notice, though, that I didn't have to prefix the module name, Tkinter, to the class name, Tk. In fact, I can now directly access any part of the Tkinter module, without having to use the module name. Since most Tkinter programs involve many references to classes and constants in the module, this saves a lot of typing and makes code easier to read.

TRAP

You can have only one root window in a Tkinter program. If you create more than one, you're bound to freeze up your program as both root windows fight for control.

Modifying a Root Window

Next, I modify the root window using a few of its methods:

 # modify the window root.title("Simple GUI") root.geometry("200x100") 

The title() method sets the title of the root window. All you have to do is pass the title you want displayed as a string. I set the title so that the text Simple GUI appears in the window's title bar.

The geometry() method sets the size of the root window. The method takes a string (and not integers) that represents the window's width and height, separated by the "x" character. I set the window's width to 200 and its height to 100.

Entering a Root Window's Event Loop

Finally, I start up the window's event loop by invoking root's mainloop() method:

 # kick off the window's event loop root.mainloop() 

As a result, the window stays open, waiting to handle events. Since I haven't defined any events, the window doesn't do much. But it is a full-fledged window that you can resize, minimize, and close. Feel free to give it a test drive.




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