SpaceBit / Computers / Python and wxPython

| Subcribe via RSS

Python and wxPython

October 26th, 2008 Posted in Computers by Ken Mankoff

I’ve recently started developing code with the Python programming language. Prior to coding I had to install the language (Python) toolkits (wxWidgets and wxPython) and tools (py2app, py2exe, wxGlade). I have a *nix computer background so rather than downloading a binary installer I opted to build everything from source on my OS X box. It took quite a while to get it all set up correctly, so I’m documenting it here in case anyone else finds themselves in the same situation.

My goal: cross-platform GUI development and application distribution using Python as a base. Keep it free, open source, and have the environment be easily removable and replicable.

There are a lot of Python window toolkits but the only two that seem worth considering are PyQt based on Qt and wxPython based on wxWidgets. I planned to do an evaluation of both, but spent long enough getting wxPython up and running that I’ve decided to code this first application with that. If I have another small app I’ll do it in PyQt and then will have a solid basis for comparison.

So, Python for the base. The GUI uses wxPython, and in order to distribute it I need py2app on OS X and py2exe on Windows. Steps below detail how to build and install all of these, and run test “hello, world”’s along the way.

Python

I installed python 2.5.2 as follows. It was built as a framework to work properly with Aqua. Everything goes in /opt/ so that it does not interact or interfere with the system installation at all.

./configure --prefix=/opt/python/2.5.2 \
            --enable-universalsdk \
            --enable-framework=/opt/python/2.5.2/Framework
make
make install

To use this environment the install instructions suggested I add the following to my PATH. This works for python, but various python utilities like easy_install were not found, so I set my PATH to the second statement.

# export PATH=/opt/python/2.5.2/bin:$PATH
export PATH=/opt/python/2.5.2/Framework/Python.framework/Versions/2.5/bin/python:$PATH
$which python
/opt/python/2.5.2/Framework/Python.framework/Versions/2.5/bin/python

For testing the minimum setup, I’m using the “Hello, World” program below:

print "Hello, World"

And it runs:

$ python hello.py
Hello, World

Py2App

curl -O http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py -U setuptools
easy_install -U py2app

There is a known bug that stops py2app from working unless you upgrade MachoLib like so:

easy_install macholib==dev

You can test py2app with the same hello world program used to test python.

py2app hello.py

Now test via

open hello.app

or double-clicking. Ideally, transfer the .app to another machine (clean, different architecture, different version of OS X) and double-click. Open Console.app to see the “Hello, World” print statement.

WxWidgets

I had to build wxWidgets as a shared (not static) library. This means wxWidgets C code won’t run cross platform without some extra work to manually install the libraries into the Framework folder in the .app bundle. But I’m not interested in wx code as much as wxPython code. If I built wxWidgets static, it would be double-linked from wxPython and would not work. Build wxWidgets like so:

export WXDIR=/Users/mankoff/local/wxPython-src-2.8.9.1
export WXPORT=mac
cd $WXDIR
mkdir bld
cd bld
../configure --prefix=/opt/wx/2.9

                 --with-mac
                 --with-opengl
                 --enable-debug
                 --enable-debug_gdb
                 --enable-geometry
                 --enable-graphics_ctx
                 --enable-sound
                 --with-sdl
                 --enable-mediactrl
                 --enable-display
                 --enable-unicode
                 --with-libjpeg=builtin
                 --with-libpng=builtin
                 --with-libtiff=builtin
                 --with-zlib=builtin
                 --enable-universal_binary
                 --with-macosx-version-min=10.4
                 --enable-shared
                 --with-macosx-sdk=/Developer/SDKs/MacOSX10.4u.sdk
make
make -C contrib/src/gizmos; make -C contrib/src/stc
make install
make -C contrib/src/gizmos install; make -C contrib/src/stc install

Add the following to your path to be able to use the wx toolkit:

export PATH=/opt/wx/2.9/bin:$PATH

wxPython

wxWidgets was built above but not wxPython. The two are included in the same tarball. To build wxPython issue these commands:

cd $WXDIR/wxPython
python setup.py install

wxPython comes with sample code, including a simple.py sample. Run

py2applet simple.py

and the resulting .app should run on both on a 10.5 Intel box and a 10.4 PPC box, and various other systems. Note that py2applet won’t work for a more complex project, and we’ll need to use a setup.py script and run it with python. A later post will cover this setup.

At this point, I’m able to code in python and wxPython and develop GUI applications that run native on multiple OS X architectures.

wxGlade

Numerous GUIs for GUI building exist. wxGlade is free, open source, and works well enough. I tested one or two others and preferred wxGlade. Download and unzip/untar it, and just run

python wxglade.py

and it should work. The tutorial is simple yet thorough.

Windows

With a complete development environment set up on OS X it is time to add a second platform.

  • Download and install the Windows distributions of Python, wxPython, and py2exe
  • To add Python to your path:
  • Right click on “My Computer”
  • Select Properties > Advanced > Environment Variables
  • Under “System Variables” scroll down to find PATH. Double-click
  • Add “;C:\Python25″ to the end

Hello world on Windows is the same as on OS X:

print "Hello, World"

And to run it, I use cmd.exe (not command.exe):

] python hello.py
Hello, World

While OS X could build simple apps easily via py2applet, py2exe requires a setup.py script. The most basic one will do:

from distutils.core import setup
import py2exe
setup(console=['hello.py'])

then

python setup.py py2exe

And you should have a “hello.exe” in the newly created “dist” subfolder. You might not see much when you run it because it prints “Hello, World” to the console and exits very quickly, but it should work.

If you want a slightly large code example with a window, use this as hello.py:

import wx
class MainWindow(wx.Frame):
    """ We simply derive a new class of Frame. """
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(200,100))
        self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
        self.Show(True)
app = wx.PySimpleApp()
frame=MainWindow(None, wx.ID_ANY, 'Small editor')
app.MainLoop()

And this as setup.py:

from distutils.core import setup
import py2exe,sys,os

origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
        if os.path.basename(pathname).lower() in ("msvcp71.dll", "dwmapi.dll"):
                return 0
        return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL

setup(windows=['hello.py'])

repeat the build command

python setup.py py2exe

And double-clicking on “hello.exe” in the build directory should launch a small window

Linux

I’m not coding for Linux at this point so I won’t cover this OS.

Summary

After following the above commands you should have a development environment on OS X that allows you to code applications in python and wxPython and assist GUI building with wxGlade. Code developed here should be compilable on Windows, and applications from either platform should work when distributed as stand-alone applications.

Future Work

  • More complex code will required a setup.py on OS X
  • setup.py can be coded so that one setup.py works on two platforms
  • Windows distribution requires installing some DLLs

Leave a Reply