■ GUI code has a bad reputation for being messy and hard to maintain. This can be overcome with a little extra effort, which will pay off when it's time to make changes to your code.
■ Refactoring is the improvement of existing code. Some goals of refactoring are to remove duplication, remove magic literals, and create short methods that do only one thing. Continually striving for those goals will make your code easier to read and understand. In addition, good refactoring also make certain types of errors (such as cut-and-paste errors) much less likely.
■ Separating your data from your layout code makes both data and layout easier to work with. The standard mechanism for managing this separation is the MVC mechanism. In wxPython terms, the View is the wx.Window objects that display your data, the Controller is the wx.EvtHandler objects that dispatch events, and the Model is your own code that contains the information to be displayed.
■ Perhaps the clearest example of an MVC structure in the core wxPython classes is the wx.grid.PyGridTableBase, which is used to model data for display in a wx.grid.Grid control. The data in the table can either come from the class itself, or the class can reference another object containing the relevant data.
■ You can create your own MVC setup with a simple mechanism for notifying the view when the model has been updated. There are also existing modules within wxPython that will help you do this.
■ Unit testing is a useful way to verify the validity of your program. In Python, the unittest module is one of the standard ways of executing unit tests. In some packages, unit testing of a GUI is difficult, but wxPython makes it relatively easy to programmatically create events. This allows you to test the event handling behavior of your application from beginning to end.
In the next chapter, we'll show you how to build a small application and how to do several things that will be common to many of the wxPython applications that you will build.
Was this article helpful?