GUI programming with Python

From Exterior Memory
Jump to: navigation, search

Mac OS X: Objective C bindings

If your only target is Mac OS X, and you intend to use the Mac OS toolbox (the operating system API) a lot, you should use the Objective C bindings.

The Python version that came with Mac OS X has these bindings build in. You can simply test this by importing the objc and Foundation modules:

python -c "import objc"
python -c "import Foundation"

If you get a ImportError, it is not installed. Otherwise, they are.

The disadvantage of these bindings is that they are not distribution with Python by default. For example if you want a newer version of Python on your Mac, like one installed with MacPorts, it does not come with Objective C bindings.

For this reason, and because I wanted to stick with Python modules for GUI programming, and don't want to rule out cross platform development, I sticked to wxWidgets and py2app on the Mac.

(Note: the Python distribution does come with the Carbon module by default. My impression is that this is deprecated.)

GUI programming: wxWidgets

If you want to program a GUI for your Python scripts, you have multiple options, including TkInker, PyGTK, PyObjC, PyQt, and wxPython. I recommend to use wxPython, which is a wrapper around the wxWidgets libraries (formerly known as wxWindows). The reason is that wxWidgets is cross-platform and at the same time retains the original look-and-feel of the underlying operating system.

Installation

You can install wxPython in many ways. For example with MacPorts:

sudo port install py26-wxpython

The wxPython homepage recommends to use a "Framework build" of Python, which presumably is a Python version with Objective C bindings. However, in my experience it worked with without the Foundation module.

Tutorials

There are many tutorials on either the wxPython or wxWidget website. I recommend the Building a wxPython Application on the Getting Started page of the wxPython website.

Here is your first program, myapp.py:

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

You can simply run it with

python myapp.py 

Mac OS X Application: py2app

On Mac OS X, you can use py2app to convert a Python script to a stand-alone executable.

Prerequisites

I have not seen any fundamental limitations for py2app, but you should decided on the target Python version before you install it. I wanted to use Python 3.1, but most tools are not available, and py2app my not (yet) work with Python 3 either, so I choose Python 2.6 which at least is forward compatible with Python 3:

sudo port install python_select
sudo port install python26
sudo python_select python26

Installing

Unfortunately, the version on pypi and Macports is 0.3.6, which does not work on Mac 10.5 (it tries to copy a file version.plist, which no longer exists).

So my recommendation is to download the subversion version and install that. py2app depends on modulegraph, so that needs to be installed too:

svn co http://svn.pythonmac.org/modulegraph/modulegraph/trunk modulegraph
cd modulegraph
sudo python setup.py install

svn co http://svn.pythonmac.org/py2app/py2app/trunk py2app
cd py2app
sudo python setup.py install

You may want to create a symlink to the py2applet tool, if you don't like long paths. Assuming that you installed it for a MacPorts version of Python:

sudo ln -s /opt/local/Library/Frameworks/Python.framework/Versions/Current/bin/py2applet /usr/local/bin/

First Application

Assume that we have an application, myapp.py.

To create the py2app configuration file, run:

py2applet --make-setup myapp.py

This creates the configuration file setup.py

To create the application, run:

python setup.py py2app

This will create the application "myapp.app" in the dist folder.

Windows Application: py2exe

On Windows, you can use py2exe to convert a python script to a stand-alone executable. I have never tested this.