Tuesday, 10 March 2009

Microsoft Windows Python GUIs


I needed to write a little GUI program for our testers today and my requirements were:
  • It must run on MS Windows XP.
  • No other programs should need to be installed.
  • It should sport a native conformant GUI.
  • It should be able to run commands on a remote linux server.
Prerequisites
I decided to use python, since I started learning it recently, and here's the complete list of additional required software I used to develop the application:
  • python
    Install python version 2.5.x on Windows. There are problems with py2exe and Python 2.6.x.
  • wxpython
    Provides the python bindings to wxWidgets, and gives the application native look and feel.
  • py2exe
    A nice utility I found that bundles all application file dependencies into a single directory. This means that neither python, nor wxpython (or anything else) needs to be installed on the client Window's machines.
  • wxGlade
    A Graphical User Interface designer.
  • pythoncomplete
    Omni-completion python plugin for vim.
  • plink
    The plink.exe executable is part of the Putty suite and I used this for scripting ssh connections. There are some other solutions that are a more complex to implement, namely: Twisted - an event-driven networking engine written in Python, Paramiko - implements the SSH2 protocol for secure connections to remote machines and Pexpect - spawn a child application and control it as if a human were typing commands.
Development
I wrote the program using vim and wxGlade. To get rid of the python console window name the python file with the extension .pyw instead of just .py, but it is very useful to use the .py file whilst developing the application.

Packaging
Once the program was written and tested I just needed to run py2exe to turn it into an executable bundle of files. A file called setup.py was required:
from distutils.core import setup
import py2exe
setup(windows=['cfak.py'])
There's a good write up on using py2exe at wxPyWiki. In a console I ran py2exe:
python setup.py py2exe
A new directory called dist was created that contained a whole bunch of files plus cfak.exe. I also copied the plink.exe file into the dist directory. Double-clicking on cfak.exe started the program.

I didn't have time to create an installer, but I've used nsis before and this is what I would have used.

Caveats
The cfak.exe ran fine, but displayed with the older style (classic style) windows controls. To get the new style I copied the python.exe.manifest file in the C:\Python25 directory to the dist directory as cfak.exe.manifest.

I needed to download and copy msvcp90.dll to my WINDOWS\system32 directory, but py2exe told me this. It also tells you what files it won't package, due to licensing restrictions. I found that I didn't need any of these extra files on the target XP machines.

Conclusion
It took me two days to write this program, which, considering I had never written a GUI program in Python before, was not too bad but not as quick as I'd hoped. Most of my time was spent fixing little problems and sorting out my development environment so my next GUI coding task should be a lot quicker.

I changed the GUI layout many times and wxGlade worked perfectly here, never overwriting my code. Although wxGlade did crash a few times it always managed to recover my last session, so crashes didn't bother me too much. I'm going to try using xrc next time however as I really do like the separation between code and GUI layout.

Overall I would rate the experience as a whole as being productive and fun! The users certainly did not complain, preferring not to touch a linux command line if at all possible.

No comments:

Post a Comment