Cairo Integration with wxPython - Installation of Python, wxPython, pyCairo, and Cairo 2-D graphics library on Windows XP
I wanted to get the Cairo 2-D graphics library to work within wxPython. I spent a few hours trying to figure out how to get the pyCairo package properly installed in Python on my Windows XP machine, so I figured I would post a detailed procedure for everyone's enjoyment.
The procedure takes about 15 minutes from not even having Python installed on your system to being up and running with Python, wxPython, pyCairo, wxCairo, and Cairo. It's pretty much entirely drag-and-drop, but I have included extra details for those who may not know what is actually going on but would like to know.
What we are working with: Python is an interpreted programming language. The wxPython package provides a cross-platform GUI library for Python. On Windows machines, Cairo is a DLL that provides a 2-D graphics library. The pyCairo package provides Python wrappers to allow Python scripts to access the Cairo DLL file. The Cairo DLL file requires two additional DLLs: libpng and zlib. wxCairo is a library that is packaged with wxPython, so don't worry about it until you actually have to start coding.
Download the following packages:
- Python 2.6 Installer - python-2.6.2.msi - From http://www.python.org/download/ - This is installer for the core Python and IDLE IDE installer. I am using Python 2.6 as the other required packages do not currently support later versions of Python (like Python 3).
- wxPython 2.8 Installer - wxPython2.8-win32-unicode-188.8.131.52-py26.exe - From http://www.wxpython.org/download.php - This is the installer for the cross-platform wxPython GUI library.
- wxPython 2.8 Docs and Demos Installer - wxPython2.8-win32-docs-demos-184.108.40.206.exe - From http://www.wxpython.org/download.php - This is the installer for documentation and demo code for the wxPython GUI library.
- pyCairo 1.8 Installer - pycairo-1.8.4.win32-py2.6.exe - From http://wxpython.org/cairo/ - This is the installer for the pyCairo package that wraps the Cairo DLL.
- Cairo DLL - cairo_1.8.6-1_win32.zip - From http://wxpython.org/cairo/ - This is a Zip file containing the Cairo DLL.
- libpng DLL - libpng_1.2.34-1_win32.zip - From http://wxpython.org/cairo/ - This is a Zip file containing the libpng DLL that the Cairo DLL depends on.
- zlib DLL - zlib123-dll.zip - From http://wxpython.org/cairo/ - This is a Zip file containing the zlib DLL that the Cairo DLL depends on.
Install everything in this order (feel free not to if you know what you're doing):
- Double-click on, run python-2.6.2.msi and agree to all defaults. This will install the Python interpreter in C:\Python26\.
- Double-click on, run wxPython2.8-win32-unicode-220.127.116.11-py26.exe and agree to all defaults. This will install the wxPython package to C:\Python26\Lib\site-packages\wx-2.8-msw-unicode\
- Double-click on, run wxPython2.8-win32-docs-demos-18.104.22.168.exe and agree to all defaults. This will install the docs and demos for wxPython to C:\Program Files\wxPython2.8 Docs and Demos\
- Double-click on, run pycairo-1.8.4.win32-py2.6.exe and agree to all defaults. This will install the pyCairo Python wrapper package for Cairo to C:\Python26\Lib\site-packages\cairo\
- Open cairo_1.8.6-1_win32.zip and extract only bin\libcairo-2.dll to C:\Python26\Lib\site-packages\cairo\libcairo-2.dll (note that the folder in the above path is "Lib", not "libs")
- Open zlib123-dll.zip and extract only zlib1.dll to C:\Python26\Lib\site-packages\cairo\zlib1.dll
- Open libpng_1.2.34-1_win32.zip and extract only bin\libpng12-0.dll to C:\Python26\Lib\site-packages\cairo\libpng12-0.dll
Your C:\Python26\Lib\site-packages\cairo folder should now contain the following files:
Change your computer's PATH variable so that pyCairo can easily find the DLLs that you just installed in its own folder:
- Get yourself to the PATH variable editing box. Right click on "My computer" -> Properties -> Advanced tab -> Environment Variables button -> Select Path in the System Variables selector box -> Click the Edit button
- Add the locations of the Python 2.6 executable (not necessary for Cairo, but this makes it a lot easier to run Python scripts from the command line) and the Cairo DLLs (so Cairo will work) to the Windows Path variable list by appending (adding to the end of the existing list) the following to the list (don't forget to separate everything with a semi-colon)
- Click OK
- Click OK
Check to see that you have installed everything correctly by double-clicking on the Cairo demo script to run it:
C:\Program Files\wxPython2.8 Docs and Demos\demo\cairo.py
A very fancy window entitled "RunDemo: Cairo" with shapes, an image, and text should pop up. If so, congratulations! You're done!
If things don't work, you will probably see a message that says the following when you run cairo.py:
Sorry. This demo requires the Pycairo package, or there is some other unmet dependency.
You will see the same message if you try the Cairo demo from within the wxPython demo program (demo.py).
To see more of Cairo in context run the main wxPython demo (C:\Program Files\wxPython2.8 Docs and Demos\demo\demo.py) and select Cairo or Cairo Snippets under the Miscellaneous section in the right sidebar that pops up.
I hope this has helped you to get started with Cairo!