16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 12<br />

■ ■ ■<br />

Graphical User Interfaces<br />

In this chapter, you learn how <strong>to</strong> make graphical user interfaces (GUIs) for your <strong>Python</strong> programs:<br />

you know, windows with but<strong>to</strong>ns and text fields and stuff like that. Pretty cool, huh?<br />

There are plenty of so-called “GUI <strong>to</strong>olkits” available for <strong>Python</strong>, but none of them is<br />

recognized as the standard GUI <strong>to</strong>olkit. This has its advantages (greater freedom of choice) and<br />

drawbacks (others can’t use your programs unless they have the same GUI <strong>to</strong>olkit installed;<br />

fortunately, there is no conflict between the various GUI <strong>to</strong>olkits available for <strong>Python</strong>, so you<br />

can install as many different GUI <strong>to</strong>olkits as you want). This chapter focuses on one of the most<br />

mature cross-platform GUI <strong>to</strong>olkits for <strong>Python</strong>, called wx<strong>Python</strong>.<br />

An Example GUI Application<br />

To make things easier <strong>to</strong> follow, I use a running example throughout this chapter. Your task is<br />

<strong>to</strong> write a basic program that enables you <strong>to</strong> edit text files. Writing a full-fledged text edi<strong>to</strong>r is<br />

beyond the scope of this chapter—we’ll stick <strong>to</strong> the essentials. After all, the goal is <strong>to</strong> demonstrate<br />

the basic mechanisms of GUI programming in <strong>Python</strong>.<br />

The requirements for this minimal text edi<strong>to</strong>r are as follows:<br />

• It must allow you <strong>to</strong> open text files, given their file names.<br />

• It must allow you <strong>to</strong> edit the text files.<br />

• It must allow you <strong>to</strong> save the text files.<br />

• It must allow you <strong>to</strong> quit.<br />

When writing a GUI program, it’s often useful <strong>to</strong> draw a sketch of how you want it <strong>to</strong> look.<br />

Figure 12-1 shows a simple layout that satisfies the requirements.<br />

269

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!