Saturday, April 15, 2006

Book Review: wxPython in Action

One of the interesting frameworks the Python programming community provides is an open source graphical user interface (GUI) framework called wxPython. Like other GUI frameworks wxPython has an extensive library of widgets and controls for building windowed applications. In addition to this, wxPython gives a programmer the ability to write one GUI application that will run on all three major operating systems: Microsoft Windows, Mac OS X, and Linux. Not only that, but depending on what platform the code is running, it will automatically morph itself to the look and feel of that platform.


While this technology is exciting, learning how to use it is an entirely different matter. As anyone who has tried to use wxPython knows, the documentation for it is rather sparse. So much so, that the recommended approach seems to consist of copying code from examples within the wxPython distribution, and then coding and experimenting with it to divine the inner workings of all its parameters and effects. This approach was way too tedious and time consuming for me, so I put off learning this corner of Python -- until now.

Recently this subject has finally gotten its due, thanks to a book and ebook published by Manning Publications entitled: wxPython in Action. The book was written by Noel Rappin and Robin Dunn. Noel is a senior software engineer for Motorola, and Robin is the creator and maintainer of wxPython; two people who have had extensive experience using and supporting the wxPython platform.

The book is divided into three major parts: the first gives an introduction to the basic structure and function of a wxPython GUI application. It looks at some of the basic widgets, discusses the principles of event-driven programming and explains the use of a tool called PyCrust to analyze GUI-based applications. The first part also outlines the use of the Model/View design pattern, and looks at re-factoring and testing GUI applications. Part 1 then ends with a step-by-step development of a sketch pad application.

Part 2 gives more detail about the implementation and use of the GUI elements briefly discussed in Part 1. Separate chapters are devoted to basic controls (ie. text boxes, buttons, etc), standard frames and dialogs and GUI layout and drawing.

Part 3 examines some advanced elements like list, grid and tree controls. It also looks at incorporating HTML and printing into wxPython applications. Finally, Part 3 concludes with topics on implementing cut and paste, drag and drop, and multithreading functions.

With few exceptions the book takes its title to heart. Offering a very fast, cogent, and concise explanation of the many widgets and tools necessary to implement a polished GUI application. One of the great features of this "in Action" book is the nesting of a functional index (in the form of frequently asked questions) within the table of contents. This makes finding recipes for common GUI widgets and techniques easy to find. For example, if one needed to quickly learn how to enter numbers, look up the chapter title "Working with the basic controls", then look under the subheading "Entering and displaying numbers". Under here one will find frequently asked questions like: 'How do I make a slider?' or 'How do I make those neat up/down arrow buttons?'.

Under each of these questions, the author provides a relatively short explanation of the widget function, and then illustrates its implementation with a mini-GUI application. Copious annotations are often provided for a complex code snippet; explaining how individual wxPython method calls work. Attribute and style tables are also included to describe how a widget's look and behavior can be altered. The book seems to include just about every feature/widget you would need to create a polished end product.

I also sat down and tried out several of the mini-GUI applications in the book. The annotations provided with the code made learning the GUI techniques relatively easy. While I was not able to run every code example in the book, the ones I did run were bug-free. For those who don't like to type in the code examples, you can download all the code examples at the Manning Publications website.

If you do find something missing, buggy or confusing, you can turn to Manning Publications' Author Online Forum. Here, you can submit questions directly to the authors and read their responses.

Manning also sells an ebook version of this book for about half the softcover price. The ebook appears to be an identical copy of the print edition with a few extra features added to it. Since the book is in the PDF format, it makes some use of PDF's ability to link to other parts of the text. For instance, you can have the table of contents presented side-by-side with the text and jump from one section to another by clicking on the various chapters and sub-headings in the table of contents. The index is also linked to the text, with each page number linked to its respective page.

One feature I would like to see added to ebooks is a link from a word or phrase in the main text to its explanation in another part of the text.

As great as this new book is, I did have a few criticisms.

In Part 1, "Introduction to wxPython", some chapters seem to provide too much information, especially for users who have had no experience with GUI frameworks. While Chapter 1 lives up to the book's "in Action" presentation style, some parts of Chapter 2 do not. In the first four sections of Chapter 2, the book gets bogged down in an extremely detailed discussion about opening application objects, redirecting output, and shutting down applications. As a result, the text here seems rather dense, and makes the going rather slow. The majority of the material presented in the first half of Chapter 2 could have been included in a later, advanced section of the book, or in an Appendix. Fortunately, the second half of Chapter 2 gets back to the "in Action" style.

In Chapter 3, "Working in an Event-Driven Environment", more or less the same writing problem happens again. The reader get exposed to lots and lots of detail about event-driven programming issues before seeing any code. The presentation once again made me feel as if I was reading a dense reference manual. In this case, I had to read through almost nine pages of text before I saw a coded implementation of event binding. This was way more detail than I needed to understand basic event binding. It was also more detail than I was going to need for the sketch pad example in Chapter 6. Unfortunately, this dense and overly detailed treatment of the event-driven environment went on for the rest of Chapter 3.

In Chapter 4, "Making wxPython Easier to Handle with PyCrust", the prose is again dense and long on detail. Given the relatively simple examples in this book, I didn't feel a chapter on using PyCrust, a program analysis tool, was needed. Even more surprising, this tool doesn't seem to be mentioned again in the rest of book. While I agree PyCrust is useful, I feel its discussion should have been placed in an Appendix. A similar problem appears in Chapter 5, "Creating Your Blueprint". Here the authors discuss the Model/View architecture, testing, and re-factoring of GUI based programs. Most of this material seems too advanced for this part of the book, and would be better suited to an Appendix.

If you are new to GUI development and want to avoid some of the extra information in Part 1, I would recommend you read all of Chapter 1, then skim Chapters 2 and 3 for the basic concepts that are presented, then skip Chapters 4 and 5, and head directly to Chapter 6. Engineers with previous GUI development experience can probably skip Chapters 2-5, then read Chapter 6.

wxPython in Action from Manning

Please don't let these few criticisms deter you from buying this book. Overall, wxPython in Action does a great job of pulling together a tremendous amount of material. And for the most part, the writing is easy to read, concise and cogent. I think this a great first effort for both authors, and an extremely useful resource for the Python community.

Buy this or any Manning book from the publisher, and get the ebook for free. You may also buy the ebook alone for only half the book price. Click here to order and support this site!

0 Comments:

Post a Comment

<< Home