macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Graphical Toolkits for OS X: wxPython

by Jeremiah Foster
04/24/2007

This series of articles will present an overview of graphical toolkits for Apple's OS X. OS X is a modern POSIX-compatible operating system with arguably the best human-computer interface ever created. But there are other GUI toolkits available aside from Apple's set of tools, and a number of good reasons to use them together with the famed Apple interface. For example, you may want to bring a UNIX or Linux application to OS X. Perhaps you have a favorite application you cannot find with fink or Darwin ports or one of the other porting tools. With the graphical toolkits I will examine in this series, you can port the code of your favorite app quite easily and bring it to the Mac. Or perhaps you know a programming language like Python, Perl, or even PHP from your web development experience, and now you want to develop a desktop application. These tools will allow you to use your favorite programming language and leverage the time and effort you have already invested in learning a scripting language. Lastly, it is also a fun and relatively easy way to teach yourself how to program for your favorite operating system.

In separate articles I will cover three excellent technologies that allow you to write desktop and HTML applications for Apple's OS X. These toolkits--wxPython, GTK+, and Qt--are all mature, widely used technologies. They all run on every major operating system (and some others besides) and have large libraries of code as well as active communities that will allow you to develop nearly any sort of application. Furthermore, they allow you to create applications with scripting languages, as opposed to compiled languages, speeding development and avoiding long compile times (though if you wish to use C, C++, or other compiled languages, that of course is supported). This article will focus on wxPython, and I'll cover GTK+ and Qt in the coming weeks.

Apple has lots of well-written documentation for OS X development, though of course most of it is directed to developing applications for OS X using its native compiled tools like Carbon and Cocoa. Nonetheless, as Apple says:

Mac OS X supports all of the mandatory POSIX APIs as well as provides a number of libraries common to System V UNIX, BSD, and Linux so that building portable code is easy.

Of the three technologies or toolkits we'll look at, at least one requires X11, an implementation of the X Windowing System. X11 is an excellent alternative to using Apple's Aqua, although it is nowhere near as user-friendly. There are a large number of programs that have been written for X, and X11 can run many of these with some adjustments. Here's what Apple says about X11:

X11 extends the BSD environment by adding a set of programming interfaces for creating graphical applications that can run on a variety of UNIX implementations. The X11 environment is especially suited for developers who want to create graphical applications that are also portable across different varieties of UNIX.

wxPython

Apple has a fairly old page that lists a number of toolkits for creating applications on OS X with X11. If this series of articles does not present a toolkit you like, you can look there to see if there is another technology you might be interested in. On that page, Apple links to wxWindows. That link goes to a web site called wxWidgets, but we'll use wxPython since we are interested in scripting languages for the Mac, so I am going to refer to it as wxPython throughout the rest of this article. Just to clear up any confusion, wxWidgets is the name of the graphical toolkit, it used to be wxWindows, and the Python bindings are called wxPython because wxWidgets is written in C++.

We use wxPython because wxPython "gives you a single, easy-to-use API for writing GUI applications on multiple platforms that still utilize the native platform's controls and utilities." Furthermore, wxPython works on a lot of older versions of OS X, so if you are running Panther, for example (as I am for this particular article), wxPython should work for you.

The first thing we need to do is download wxPython so we can begin creating applications. I ran into trouble compiling the source code on my old laptop, so I opted to follow advice on the mailing list and download the disk image instead. Downloading the disk image gives you a familiar way to install the wxPython software, at least if you have previously installed software on your Mac. Since I am running 10.3.9, I chose to download the prebuilt binary package labeled osx-unicode-py2.3. As you can see from the name of this binary, there are a lot of things to take into consideration. They are:

  • Which version of python are you going to use
  • Which operating system version you are using
  • What type of chip you are running
  • Whether you want ansi or unicode string handling

This makes things more confusing than they necessarily have to be, but let's see if we can clarify the situation a bit.

Python comes in many versions, the latest of which is 2.5. It is always best to use the latest version if you can. The OS you are using is of course referring to Apple's OS X, but the version of OS X (i.e., Panther, Tiger, Jaguar) is an issue. If you are using version 10.3.9 of Mac OS X, then use the precompiled binaries labeled OS X 10.3 PPC, coordinating your version of OS X with your version of Python. Of course if you are running Tiger 10.4 and an Intel chip, then you can use the 10.4 universal binaries.

Finally, we have ansi versus unicode. If you are just trying to get started with window programming, you do not need to deliberate endlessly over this decision. Essentially what has happened is that wxPython has inherited Python's distinction between unicode objects and string objects, with unicode objects being 16 or 32 bit (depending on platform), and string objects being 8 bit. If you chose the ansi build and need to use unicode, you will have to convert from ansi to unicode. You can use the unicode build and obviate the need for conversion if you know in advance you will encounter unicode, which seems more and more likely these days. In my case, the choices boiled down to OS X 10.3.9 prebuilt binary with unicode, but you can install more than one binary if you wish.

Once we have chosen a binary and downloaded it, we are presented with a familiar disk image. This is mounted when we double-click on the icon, presenting us with a readme file and the software package. The disk image also courteously contains a simple Python script to remove the wxPython installation or to manage your installations of wxPython. There is info in the readme about multiple wxPython installations, but we are not going to cover that here. Now double-click on the package icon and unpack the software. This brings up Apple's installer, which will install our software, but before we do, note that the installer tells us:

You must install onto your current boot disk, even though the installer does not enforce this, otherwise things will not work.

So we dutifully install onto the boot disk and follow along as the installer works its magic. Once you have all the software installed you can quit the installer, but how do we know we were successful? Let's try to run a program to test our new installation. A perfect candidate for this is the small program included in the wxPython tutorial located here. In true open source style I have blatantly stolen the code, and we are going to use it here.

#!/usr/bin/env pythonw
import wx
app = wx.PySimpleApp()
frame = wx.Frame(None, wx.ID_ANY, "Hello World")
frame.Show(True)
app.MainLoop()

However, I made one change to the code that you will want to make as well. So open your favorite text editor and change the shebang line, (the line with #!) so that it reads; #!/usr/bin/env pythonw. We do this because pythonw is a binary that contains all the hooks into the Aqua windowing interface to give us that good "look and feel." Save the code as wxpyText.py, and make it executable with a simple chmod:

chmod 755

and then run it like this:

./wxpyText.py

If you see a window like the one below, then you know you were successful.

Figure 1
Figure 1. Your installation has been successful

Pages: 1, 2

Next Pagearrow