活学活用wxPython

[attachment:cover.jpg] TableOfContents

前言

关于 Harri PasanenRobin Dunn 以及wxPython的传奇故事确切的开始时间是1995年。本书的合著者之一,Robin 写下了下面这段关于wxPython 的文字,而我们决定让故事由参与者自己来讲述,而不是籍由旁人加以引述:

必须特别说明的是:SWIG 使得我能够轻松创建和维护成千上百行的代码,因此wxPython的多种功能特性必须感谢David Beazley以及其他项目参与者对SWIG的贡献。通过此书,我们希望能够与您分享对于wxPython的热情,在轻松开发图形用户界面应用程序方面,它是真正的独一无二的开发包。我们写作的初衷是不但为初学者,也为专业人员建造一项有用的资源。

acknowledgments

Our work on this book has been aided by a number of people, in ways both obvious and subtle.

Patrick O’Brien was instrumental in getting this project off the ground and Marjan Bace, our publisher at Manning Publications, gave us the chance to make it happen. Jackie Carter was our first editor at Manning and Lianna Wlasiuk came in at a crucial juncture, and, like a great bullpen pitcher, earned the save. We would like to thank our production team at Manning and every-one who worked behind the scenes on the book, especially our copyeditor Elizabeth Martin and our typesetter Denis Dalinnik. Big thanks also to our agent Laura Lewin for her help and understanding.

We’ve benefited a great deal from the generosity of programmers world-wide in creating the technologies used in this book. First on that list is the crew who developed the wxWidgets foundation of wxPython: Julian Smart, Vadim Zeitlin, Robert Roebling, and the rest of the core wxWidgets team, as well as many contributors. Of course, without Guido van Rossum and other members of the Python language development team, there would be no “Python” in “wxPython.” A large part of this book was written using the jEdit text editor and the images were manipulated using the GIMP.

Thanks also to the many reviewers who looked at the manuscript in its vari-ous stages of development and added their valuable insight along the way. They helped make this a better book: Alex Martelli, Dave Brueck, Christopher Bailey, Mike Stok, Jean Baltus, Thomas Palmer, Jack Herrington, Peter Damoc, Nick Coghlan, Pim Van Heuven, John Paulson, Ian Brown, Stefan Neis, Chris Mellon, Kevin Ollivier, and Doug Tillman. Special thanks to Clint Howarth who was our technical proofreader.

NOEL RAPPIN My wife, Erin, makes my life better every day. Thank you for your strength, your intelligence, and your love. I also want to thank my parents Donna and Donnie and my sister Michelle for supporting me fully and com-pletely. Matt Cohen helped this effort by entertaining me via IM while I was try-ing to write. Finally, I want to thank my daughter Emma, who is now old enough to be excited to see her name in a book, and my son Elliot, in anticipation of the day he will also be glad to see his name.

ROBIN DUNN I’d like to thank my wife, Malaura, and my children, Jordyn, Myka, Samuel, and Christian, for their patience, understanding, and love. You make it worth the effort to get past all the difficulties of life. I’d also like to thank the many wonderful members of the wxPython user community whose praise and excitement have helped me to stay motivated and have helped me to take wxPython from an interesting toy to a very useful tool that thousands of develop-ers all around the world love and depend upon. And finally, many thanks go to Mitch Kapor, John Anderson, David Surovell, and others at the Open Source Applications Foundation for believing in the Open Source concept, the poten-tials of wxPython, and especially for believing in my capabilities and funding my work on wxPython for several years.

about this book

Who should read this book?

Naturally, we’d love everybody to read this book. If you are reading this in the bookstore, trying to decide whether to purchase it, we say, go for it! Buy one for the people next to you too—they’ll thank you later.

How this book is organized

We’ve divided wxPython In Action into three parts. The first part is an introduc-tion to wxPython concepts, a tutorial on how to get started with wxPython, and some information on wxPython best practices. The chapters in part 1 are: Chapter 1, Welcome to wxPython In this chapter, we introduce wxPython, explain to you why it’s the greatest thing since sliced bread, and give some background on the technologies used to create wxPython. Chapter 2, Giving your wxPython program a solid foundation The two most important objects in wxPython are discussed. Every application must have an application object and a top-level window. This chapter will show you how to start a wxPython program, and how to manage its lifecycle. Chapter 3, Working in an event-driven environment Like all GUI toolkits, control in wxPython is managed by events. This chapter discusses how events are handled, and how you can use them as hooks to drive your functionality. Chapter 4, Making wxPython easier to handle with PyCrust PyCrust is a Python shell written in wxPython that contains many advanced and useful features. Not only can you use PyCrust for your wxPython develop-ment, you can wrap your program inside it for debugging purposes, and you can reuse the PyCrust components in your own applications. Chapter 5, Creating your blueprint This chapter discusses best practices in three areas that are often difficult for GUI programmers. We show how to use refactoring to improve the structure and maintainability of your code. The Model/View/Controller design pattern is explored, and we’ll show you how to unit test our GUI code to minimize errors. Chapter 6, Working with the basic building blocks This chapter is a bridge between parts one and two. Building on the basic ideas already shown, we give hints of some of the features discussed in parts 2 and 3 as we build a sketchpad application. Part 2 begins the more detailed portion of the book. The chapters in part 2 take a look at the most commonly used parts of wxPython. This includes a tour of the basic widget set, a look at standard frames and dialogs, and information on drawing and layout. The chapters in part 2 are: ABOUT THIS BOOK Chapter 7, Working with the basic controls This chapter covers the API for the basic widget set, including text fields, but-tons, list boxes, and the like. Chapter 8, Putting widgets in frames All your wxPython widgets will be inside a frame or a dialog. In this chapter we cover how frames work, what kind of frames there are, and how to manage widgets within a frame. Chapter 9, Giving users choices with dialogs Dialogs behave slightly differently than frames. We cover how modal dialog boxes work, as well as the standard predefined wxPython dialogs. We’ll also show you how to use wxPython validators to help mange the data in a dialog. Chapter 10, Creating and using wxPython menus Most windowed applications have a menu. We’ll show you how to add menus to the menu bar, and menu items to a menu. Specialized menus, such as checkboxes, and radio menus will also be covered. We’ll also discuss keyboard shortcuts and some usability guidelines for using menus effectively. Chapter 11, Placing widgets with sizers In wxPython, sizers are used to spare you the drudgery of placing your wid-gets manually. There are several useful sizers that are part of wxPython, and we’ll show you how to use them, and what kind of layout is best suited to each. Chapter 12, Manipulating basic graphical images The most basic purpose of any UI toolkit is to draw lines and shapes to the screen. In wxPython, there is a rich set of drawing tools available for your use. There is also a powerful abstraction called a device context which allows you to draw to a target without caring whether the target is a window, a printer, or a file. Part 3 contains a detailed look at more advanced portions of wxPython. It starts with a description of the three most complex wxPython widgets, and continues with a discussion of various print and display mechanisms, closing out with a tour of useful items that didn’t quite earn their own chapter. The chapters in part 3 are: Chapter 13, Building list controls and managing items The wxPython list control gives you the ability to display lists “explorer-style,” in icon mode, list mode, or multi-column report mode. You can also custom-ize sort behavior, and allow users to edit list entries. Chapter 14, Coordinating the grid control If you want something that looks like a spreadsheet, the wxPython grid control is a full-featured widget that will meet your needs. It allows full control over the display and behavior of the grid, and allows for complete customization. Chapter 15, Climbing the tree control The wxPython tree control allows for compact display of hierarchical data, including, but not limited to a directory tree or class hierarchy. You can also allow the user to edit entries on the fly. Chapter 16, Incorporating HTML into your application Within wxPython, you can use HTML to simplify the display and printing of styled text. The HTML engine inside wxPython can also be customized to fit your special needs. Chapter 17, The wxPython printing framework Printing is managed from a wxPython application through several dedicated print, print data, and print preview objects. In this chapter, we explore how all of them work together. Chapter 18, Using other wxPython functionality In this chapter, we cover some important features that are not quite long enough to justify a chapter on their own, including cut and paste, drag and drop, and multithreading.

How to use this book

How you use this book will depend on your wxPython knowledge. We designed this book to be useful for both experts and novices, but we expect that different parts of the book will have more or less resonance for users at different levels.

Typographical conventions The following conventions are used throughout the book: Courier typeface is used in all code listings. Italics are used to introduce new terms. Courier Bold is sometimes used to draw your attention to a section of code. Code annotations are used when directing your attention to a particular line of code. Annotations are marked with bullets, such as b. Courier typeface is used in text for code words, wxPython class and method names, or snippets of Python code. Code downloads Source code for all of the examples used in this book is available for download from the publisher’s website at www.manning.com/rappin. Where to get more help Although we tried to be as comprehensive as possible, we couldn’t possibly antic-ipate all the uses and issues you might have using wxPython. The main wxPy-thon website at http://www.wxpython.org has some resources that you might visit for insight into your problem. The official online documentation is at http://www. wxpython.org/docs/api/. A collaborative wiki site is available at http://wiki.wxpy-thon.org/, and there are mailing lists that you can subscribe to at http://www.wxpy-thon.org/maillist.php.

Author Online

Help is also available from the Author Online forum, a private web discussion board run by Manning Publications. You are encouraged to use this forum to make comments about the book, ask technical questions, and receive help from the authors and other readers. Use your browser to navigate to www.man-ning.com/rappin to take advantage of this free service. The forum’s welcome page gives you all the information you need to sign up and get going.

about the title

By combining introductions, overviews, and how-to examples, the In Action books are designed to help learning and remembering. According to research in cognitive science, the things people remember are things they discover during self-motivated exploration.

about the cover illustration

The figure on the cover of wxPython in Action is a “Soldat Japonais,” a Japanese soldier. The illustration is taken from a French travel book, Encyclopedie des Voy-ages by J. G. St. Saveur, published in France in 1796. Travel for pleasure was a relatively new phenomenon at the time and travel guides such as this one were popular, introducing both the tourist as well as the armchair traveler to the inhabitants of other regions of France and abroad.

computer business with book covers based on the rich diversity of regional life two centuries ago, brought back to life by the pictures from this travel guide.