After 30 years of programming, I have recently drawn the conclusion that the Python language is the greatest thing since sliced cheese. It is very easy to learn, extremely powerful, and you can do just about anything with it, quickly and easily.
Python is very good at creating GUI apps, but it is easy to get all tied in knots if you don't have the right strategy. Read on to learn how to write GUI apps in Python.
How to Write GUI Apps in Python
The most common approach to GUI programming in Python is to use Tkinter. You can find many examples of how to do this on the web. Unfortunately it's easy to go horribly wrong with this strategy, as your application becomes more complex. I'm not going to talk about Tkinter, even though it is the most popular approach and I have managed to use it with success.
The most effective strategy I've found for creating GUI applications in Python involves a combination of the following tools:
- Instead of Tkinter, use wxPython. It may require a separate download. It contains more advanced controls (such as a tree control) that are harder to implement in Tkinter.
- The wxGlade interface designer. It is a version of the popular Glade interface designer written in wxPython. It is similar to Microsoft Visual Studio, in that you can drag and drop controls onto a window and it will generate the code to create your windows in Python.
- Your favorite Python development system. If you're not already committed to one, then try Aptana Studio with Pydev.
Programming in Python
In order to make this work, there are a couple of decisions you'll have to get right.
- Take the time to learn how to use wxGlade. It's a little tricky at first, but there are a number of good tutorials out there. Video tutorials are the easiest, since you can watch the steps and follow along in a separate window. The biggest thing to get used to in wxGlade is that you have to put in a sizer before you can add anything else Also, you will want to generate all the target code into a single file containing a custom Frame class.
- Use the following coding strategy. WxGlade will generate code for you, but the next time you change the window, it will overwrite any changes that you have made manually. To get around this, try creating a subclass of the classes that wxGlade generates, and keep them in a separate file. That way you can change the window definition any time you want to, and regenerate all the windows without losing any of your work.
- Take the time to learn wxPython. It provides many capabilities and features that you need to understand in order to make your application run smoothly. A lot of information is also available about the wx toolkit that is not Python specific. There are plenty of tutorials available, but there is only one good book on wxPython.
Using this general approach, it is definitely possible to create complex and beautiful GUI window applications in Python with a minimum of difficulty. There are many other strategies available, but this is the easiest and best one I've found so far for programming in Python.
Join the Conversation