Archive for the 'IronPython' category

Cross Platform GUI Development with Python and Qt

 | May 12, 2009 5:16 pm

One of computer programmers many fantasies (or at least one of my fantasies) is a cross-platform codebase that is easy to maintain.  I want to write an application once and then be able to deploy in on any platform in existence.  It should integrate into the destination environment in a native manner, but still be similar enough to the other platforms so that  issues can be addressed in a way that doesn’t hinder overall progress.

Part of this dream can be fulfilled by using Python and standard library, it is also possible to do much of it by using IronPython and .Net (through the OpenSource Mono framework).  However, in each case, you are forced to make a tradeoff.  Python and the standard library are wonderful, if you are willing to accept a speed hit.  IronPython and Mono also work well, as long as you avoid using any of the Windows specific libraries.  Unfortunately, many of neatest .Net features (like Windows Presentation Foundation) are only available on Windows.

Okay, there is always GTK+ or wxWidgets (the same toolkits used for the Gnome desktop), but GTK+ is written in C and while much of the miscellaneous nastiness has been abstracted away, it is still one of the hardest toolkits to work with.  Oh, and the documentation sucks.  (When any documentation can be found at all.)  Thus, we arrive at the final contender: Qt.  Qt has had a long and controversial history  involving license issues, passion, and miscellaneous geek pride.  In fact, it’s due to Qt and some rather divisive decisions made by its developers that we even have alternative toolkits at all.  But, despite that history, Trolltech’s recent decision to release the framework under the permissive LGPL license (rather than the noxious GPL) changes all that.  In many ways, Qt might just become the cross-platform toolkit of choice

Of course, it's a given that Qt is tremendously powerful with rich functionality.  Applications that are written in Qt are fully cross-platform and behave the same way wherever they are deployed.  It uses native widgets whenever possible and has a sophisticated theming system that allows it to follow platform conventions without significantly modifying the underlying code.  More importantly, Qt is backed by a passionate developer community and a well funded corporation (Nokia).  It is true, you get what you pay for.  Qt is well designed and the documentation is simply fantastic.  And unlike GTK+ or wxWidgets, the entire toolkit is written in proper object oriented C++.

I first started experimenting with the Qt framework about two months ago, after deciding to tackle a major add-on to one of my favorite writing programs, LyX.  As I have said elsewhere, I am not the world’s most

gifted computer programmer (far from it actually), but Qt actually makes it pretty easy to get yourself up and running in a reasonable amount of time.  If I were forced to do most of my GUI programming in Qt and C++, that would be an acceptable solution.  However, that is completely unnecessary.  You see, Qt has one of the most tightly integrated Python bindings available, through the user of PyQt from Riverbank software.  Qt plus Python is, quite simply, programming perfection.  So while I did some mild experimenting with the Qt frameworks in C++, I have spent much of the past few months doing the heavy work of learning the framework via the use of  Python.

In the process, I have made a great many mistakes and a great many more discoveries.  And since the final step in any discovery is to share your findings, I will be doing that through this series of articles.  Of more interest to most, however, I will also be introducing the fruits of my labors: a somewhat functional prototype of my outliner add-on for LyX.

Show me more... »

Installation of PyQt on Mac OS X

 | 5:12 pm

Note: The installation instructions in this article apply to Mac OS X 10.5 (Leopard) and Qt 4.5.3.  While it is possible to get Qt 4.5.3 to run on Snow Leopard, it requires compiling it from source.  Instructions for doing so can be found in the comments below.  It is important to note, however, that there are some minor issues with the 4.5 framework on Snow Leopard (including mutlithreaded memory leaks).

You can find instructions for how to install PyQt on Snow Leopard using the Qt 4.7 frameworks at

To install PyQt on Mac OS X, it is also necessary to install and configure the GCC compilers and the Qt framework in addition to SIP and PyQt.  The easiest way to get the most current version of the GCC compilers and tools is by downloading the XCode tools package from Apple at the Apple Developer Connection (ADC).  ADC requires that you have an Apple ID and account, which are both free.  After you log-in, download the XCode developer tools package, which is a bit hefty (nearly 1 GB in size) and may take a while to download depending on your internet speed.

XCode and Qt

While waiting for the XCode tools to download, you can also begin downloading the Qt binary installer from the Trolltech downloads site.  As with Windows, I would advise that you you select  the complete SDK (which also include pre-compiled binaries) rather than the source release.  If you only download the Qt framework, you will need to compile the source yourself which is both time intensive and is not worth the additional headache.

Once XCode has finished downloading, run the package installer.  It will ask you to select the destination drive and what components of the software you would like to install.

Select the installation disc where you want to install XCode the developer tools

Choose the components you would like to install

This will automatically install and configure the GNU tool chain.  Once it has finished, you can access the tools by going to the “/Developer/” folder on the root of the drive.

After XCode has finished, run the Qt installer.

The Qt SKD Installer

Select the installation destination

The Qt installer package will automatically place the tools and other things that you will need in the same Developer folder where the XCode tools and frameworks are located.  You will need to make a note of the path for qmake.  The default installation site for qmake is “/usr/bin/qmake-4.5”.

SIP and PyQt

SIP is a program that makes it easy to create C++ bindings program for Python.  You can find the source code at Riverbank’s SIP download page.  Since we will be using the 4.5 version of PyQt, you will need to download the latest developer snapshot (version 4.8 or greater).  Be sure to get the Linux/Unix source code, rather than the Windows source.  You will also need the latest source code snapshot for PyQt 4.5, which is also available from Riverbank.

After you have finished downloading the source files, move them to a folder in your Users directory.  I have a special directory entitled “Applications” where I keep the source code for programs that I have manually compiled.  Note: The rest of the steps will be done from the Mac OS X terminal.

After you have moved the source code for both SIP and PyQt to this new directory, extract it by using the tar utility with the x and f options (tar –xf):

cd ~/Applications
tar -xf sip-4.8-snapshot-20090430.tar
tar -xf PyQt-mac-gpl-4.5-snapshot-20090507.tar

After you have expanded the files, it might be a good idea to rename the directories to something more manageable (like sip-4.8 and PyQt4.5:

mv sip-4.8-snapshot-20090430 sip-4.8
mv PyQt-mac-gpl-4.5-snapshot-20090507 PyQt4.5

First, we need to compile and install SIP.  The default configuration will move the compiled files to a directory where Leopard can’t read them.  So, we will manually need to specify the destination directory using the –d flag:

cd sip-4.8
python -d /Library/Python/2.5/site-packages

After the configuration is finished, run make and sudo make install:

sudo make install

Once SIP has finished installing, we need to repeat the process for PyQt.  From the sip-4.8 directory, chagne to PyQt:

cd ..
cd PyQt4

Next, run the configuration script specifying the path to your version of qmake and the installation directory for the python bindings:

python -q /usr/bin/qmake-4.5 -d /Library/Python/2.5/site-packages/

Then compile and install:

sudo make install

Since Qt is a rather large framework, it may require between 15 and 30 minutes to fully compile.

Installation of PyQt on Windows

 | 2:00 pm

On Windows, we first need to install the pre-compiled Qt binary package from the Trolltech downloads page.  Select the LGPL/Free tab and choose the “Qt SDK: Complete Development Environment” for Windows.  It is a fairly hefty package (167 megabytes) and may take a while to transfer.  The installer includes the core Qt libraries, several important C++ development tools, and an integrated development environment (IDE) called Qt creator.  Luckily, the same installer will work on Windows XP, Windows Vista and Windows 7.

Qt and MinGW

When you run the installer, it will ask for the installation destination.  To avoid future problems, this folder should not include any spaces. The default is “C:\Qt\2009.02”,  but because I like to keep the root directory of my drive as clean as possible, I changed this to “C:\Qt\Windows”.  To begin the installation, press “Install.”

Change the installation directory to C:\Windows\Qt

The installer will also download and configure the GNU compiler toolset for Windows (also known as MinGW).  By default, MinGW will be installed in a subdirectory under the main Qt directory.  In this example, it was installed to “C:\WINDOWS\Qt\mingw”.  Note this file path, it will be important to the steps below.


After you finish with the Qt framework, the next step is to download and install Python.  You can get the Python interpreter and other associated files from the Python release download page.  While the newest version is Python 3.0.1, I would recommend that you download and install Python 2.5.4 instead.  When trying to set up SIP with Python 2.6.2 and Python 3.0.1, I experienced a number of errors.  These were resolved by using the x86 version of Python 2.5.

When you first run the installer, it will ask whether you want to install Python for all users or for just the current users.  Make sure that “install for all users” is selected.  Like Qt, you should install Python to a directory without spaces.  The default install location will be “C:\Python25”, though I typically change it to “C:\Windows\Python25” so that it matches the installation directory for Qt.

Make sure that the "Install for all users" option is selected

Change the installation directory to "C:\Windows\Python25" so that it is similar to the installation directory of Qt

After the Python installer has finished running, you will need to modify one of the Windows environment variables.  On Windows XP, the easiest way to access the environment variables to right click on “My Computer” and select “Properties” from the drop own menu.  When the “System Properties” window launches click on the “Advanced” tab and then press the “Environment Variables” button.   (If you are using Windows Vista or the Windows 7 beta, the easiest way to access the Environment Variables is to go to Control Panel and Search for “Path” in the search bar.  All other steps remain the same.)

You can open the "Environment Variables" by right clicking on "My Computer" and selecting "Properties."  Then choose the advanced tab and click on the "Environment Variables" button

From the “System Variables” list, select the Path Variable and then press “Edit”.

Select Path from the "System Variables" list and select "Edit"

In the variable value box, type in the path to the Python folder you just installed, the path to the “bin” folder of MinGW (which should be under the MinGW folder from above), and last the path to a Qt program called qmake (which is located in the qt\bin folder of the Qt installation) .  You should separate these values from the other paths (and from each other) by a semi-colon.  If you have followed all of the instructions in this tutorial, you will add:


And your path variable should now look something like:

%SystemRoot%\System32; C:\Windows\Qt\mingw\bin; C:\Windows\Python25; C:\Windows\Qt\qt\bin

Press “Ok” to close the dialog box and then “Ok” again to close first the Environment Variables and then the “System Properties Pane.”  Now that you have changed the Path variable, you need to restart your computer so that the changes can take effect.

SIP and PyQt

Prior to compiling PyQt, you will first need to compile and install SIP, which can be found at Riverbank Software.  Though the stable version of the software is currently (4.7.9), you will need to download the latest Windows source code  snapshot of version 4.8 (Qt 4.5 doesn’t work with earlier versions of SIP).  (Update: Alternatively, you can also find a copy of the 4.8 snapshot here.)  You will need the most recent source code snapshot of PyQt 4.5, which is also available from Riverbank.  (Update: Multiple people seem to have had problems installing the 4.5 snapshot from the Riverbank website.  As a matter of convenience, you can find the April 30, 2009 version here.  This is the same version that was used in the writing of this article.)

After you have finished downloading the source files, extract them to the Qt installation directory (C:\Windows\Qt in this tutorial).  This might also be a good time to rename the directories to something shorter than the default snapshot names.  I have changed mine to PyQt4 and sip-4.8.

The rest of the steps will be run from the command line prompt (cmd.exe) and must be run with administrator privileges to work properly.  Start by going to the start menu and then choosing the run command; then type “cmd.exe” and press enter.  (If you are on Windows Vista, you can open the command prompt by typing “cmd.exe” in the search dialog of the start menu.  To run with administrator privileges, right click on the top program choice and select “Run as administrator” from the context menu.)

At the command line, first, go to the Qt installation directory by typing:

cd C:\Windows\Qt

Then go to the SIP directory:

cd sip-4.8

Prior to compiling, we need to create a proper configuration file:

python –p win32-g++

After the configuration is done, compile the new file by typing:


This will take some time.  After the files have finished compiling, you can install them by typing:

mingw32-make install

The process is repeated for PyQt.  Change to the PyQt4 source directory by typing:

cd ..
cd PyQt4

Then configure the make files, compile and install by typing:

python –p win32-g++
mingw32-make install

Since Qt is a rather large framework, it may take between 15 and 30 minutes to fully compile.

Update: If you only want to run PyQt programs, the installation process can be greatly simplified by using the automated PyQt installer for Windows.  The installer will automatically install a copy of the Qt framework and the PyQt bindings.  You will need to install Python separately, however.

Installation of PyQt on Linux (Ubuntu)

 | 1:52 pm

In comparison to Windows and Mac OS X, installing PyQt on Linux is trivially easily.  It is likely that all of the software necessary for Python development is likely available in your favorite distribution repository.  If you are using the most recent version of Ubuntu 9.04 (and who isn't), then, all you need to do is open up your terminal and type:

sudo apt-get install python-qt4 qt4-dev-tools python-qt4-dev build-essential pyqt4-dev-tools

The package manager (apt-get) will take care of downloading and installing any dependencies.  Once the installation routine finishes, you will have a fully working installation of Qt 4.5 and PyQt 4.5 without any compilation, configuration, or other miscellaneous headaches.  The qt4-dev-tools, pyqt4-dev-tools and python-qt4-dev packages install important tools used for creating user-interfaces.  The build-essential package installs the GNU compilers and autoconf tool chain.  If you will only be running PyQt programs, these additional packages are unnecessary.

IronPython in Action

 | February 9, 2009 4:46 pm

IronPython in ActionThere is an old adage, often used and much abused, “Give a man a fish and he will eat for a day.  Teach a man to fish and he will never want for food.”  And while this old saying is extremely tired and very much cliché, it is a surprisingly apt way for me to begin a review of “IronPython in Action,” a programming book by Michael J. Foord and Christian Muirhead.

“IronPython in Action” delves into the use of scripting (or dynamic) languages and how they merge with Microsoft’s enormous .Net platform.  For readers of this site, it shouldn’t be any secret that I have been working hard to teach myself Python.  And while that particular adventure is better documented elsewhere, “IronPython in Action" has been central to it.  Indeed, it has been my primary reference manual and guide.

It’s been nearly six months and it’s far past time to write a review of the guide.  But this review is going to be a little bit different than others that you might read.  If you are looking for a more traditional summary, you might be better served by Craig Murphy’s exhaustive breakdown.  Rather than detail the contents and render an opinion, I would like to first tell a story and then focus on a much narrower question: Is IronPython valuable for people who are new to Python or .Net programming?  More to the point, can it be used to teach a novice “how to fish?”

Show me more... »

IronPython - Data Binding and Data Templates - Part 1

 | January 6, 2009 1:12 am

Figure 1. The main Podcast list for Podcatcher. This is an example of WPF data binding where a data template and data trigger have been applied. The selected item shows the feed description in addition to the feed name, link and image.

One of the most important and fundamental purposes of computers is to manipulate information. This can range anywhere from simple storage and retrieval to complex applications, like automated detection of diseased heart tissue. However, automated manipulation of data is only of limited interest. At some point, data must interact with human beings in order to be interesting. Luckily, Windows Presentation Foundation (WPF) has an extremely powerful feature meant to facilitate man's interaction with his data. This feature is called data binding. As described by Microsoft:

"Data binding is the process that establishes a connection between an application and the underlying logic. If the binding has the correct settings, when the data changes its value, the elements that are bound to the data reflect changes automatically."

Data binding has appeared in many incarnations among the various Microsoft technologies. A version of it can be found in Windows Forms as well among the various voodoo cults of web programming. Since data binding has been around for a while, the version included in WPF is a very mature technology. Tens of thousands of developers have helped Microsoft work out the rough edges and it is possible to do some rather sophisticated things.

While data binding is capable of doing sophisticated things to complex sets of information, you won't find much of that here. Rather, this article and the others in this series will focus on a very simple application: manipulating and displaying information from RSS feeds. After all, I am hardly an expert in either IronPython or WPF. Rather, I plan to stick to comfortable ground and I will be returning to my Podcast aggregator. Previously, I created a download manager to handle the work of retrieving files; now it's time to start tackling another major module: the Library. In doing so, I will compose variations on a theme. I will show how WPF displays data and facilitates user interaction with that data. This article will introduce the features of the project and provide some background on data-binding. Later articles will look at code and how to add in bells and whistles.

Show me more... »

WPF – SVG Graphics and XAML – Part 3

 | December 16, 2008 6:49 pm

As described in part 1 of this article, vector graphics offer a tremendous number of advantages over their raster counterparts. These benefits include the ability to enlarge the image to any size without a loss of detail or quality and better reproduction in both print and online form. Combined with the existence of many high quality icon libraries, vector graphics represent a valuable source of art for desktop applications.

In part 2 of this article, we looked at a way to convert vector graphics using Adobe Illustrator. While useful, the XAML export plug-in has a number of limitations and is not always able to faithfully convert the image to XAML. Thus, while Adobe Illustrator is a good conversion method when working directly with artists and graphic designers who are able to provide art files in Adobe Illustrator (AI) format, it is not so well suited to existing OpenSource libraries which tend to be distributed in the SVG format.

Due to their use of alpha transparency, Adobe Illustrator is always able to read the images from other editors, though it contains basic SVG support. This is unfortunate as these icon libraries, such as the Oxygen Icon Set, are freely available under permissive licenses. Fortunately, there is a way to overcome some of these limitations. In this article, I will look at how to convert SVG icons to XAML using the stand-alone utility XamlTune. We will also be using the OpenSource SVG editor, Inkscape.

Show me more... »

WPF – SVG Graphics and XAML – Part 2

 | December 14, 2008 1:34 am

In part 1 of this article, I introduced some of the difficulties in converting SVG graphics to WPF XAML. These included inconsistent implementations across SVG editors and the lack of a high quality XAML export. I also introduced two methods for the conversion of vector graphics to XAML. The first uses Adobe Illustrator and the second makes use of free standalone program called XamlTune. In this article will take a detailed look at the first of these methods. In part 3, we will look at the second method.

Show me more... »

WPF – SVG Graphics and XAML – Part 1

 | December 13, 2008 7:35 pm

In a perfect world, most would be judged on the quality of their ideas and depth of character. However, more often they are judged by looks and dress.  What is true in the world of people is also true in the world of software. More than one website, computer program, or presentation has suffered because it is unattractive. In the inevitable fight between style and substance, substance often exerts more than its fair share of influence.

Given the importance of stylistic presentation, it is somewhat ironic that one of the most neglected components of many software projects it that of the artwork. While the code may be carefully vetted and analyzed, often icons and other artwork are chosen at the last stage of the design. While this might be unfortunate, it is due to an important reality: high quality artwork can be very, very expensive.

Fortunately, there are a number of beautiful graphics libraries that are either cheap or free of charge. Most are available under permissive or OpenSource licenses and can even be used in commercial products. There is just one problem; nearly all of these libraries are available in formats which don't play nicely with WPF and XAML. With a little bit of planning and strategy, however, it isn't too difficult to use these resources in your own applications.

Show me more... »

IronPython – Windows Presentation Foundation Tutorials

 | December 11, 2008 3:38 pm

After finishing the download manager of my podcast aggregator (PodCatcher), I took a few days off to work on other things. After a brief break, though, I am back at the grindstone and I can happily say that it is starting to come together.

The next major component of the aggregator that I will work on is the library module. This will include the functions necessary to view, add, remove and manage the different Podcast feeds and the individual podcast episodes. Due to my initial architecture, I have hit a patch of luck.

Since the aggregator sits on top of the Windows RSS Platform, much of the "library" functionality is already handed to me. Further, because Internet Explorer uses the RSS platform and the Common Feeds List, there will be a nice integration with other programs. The ability to subscribe to new podcasts from the browser always made more sense to me than the model of other aggregators, where management occurs exclusively in the Podcast Manager.

Since much of the backend is already written, I have been thrown into a realm of programming where I have done very little work: the user interface. While I touched on the user interface a little in the Learning IronPython series, I have now gotten serious about it. The screenshot shows the subsequent result. I have a prototype user interface for PodCatcher!

While putting the UI together, however, I discovered a bit of a surprise.  According to the Oracle of Google, there isn't a lot of material on WPF and dynamic languages. What is worse, getting the UI to load and function isn't as simple as applying stock C# principles. To answer my (many) questions I have spent quite a bit of time hunting down tutorials and looking at everything I can find about IronPython and WPF. After trundling through Google, the IronPython users archive, MSDN, and the major IronPython blogs, I have a pretty good list and I thought it might be of interest to others trying to learn IronPython.

Show me more... »