Tuesday, July 31, 2007

First bits of PyQt

Yesterday I wrote my first PyQt application. It is really simple program, called "tray", which lives in system tray and notifies user of new messages at my favorite social network Vkontakte.ru (Russian clone of FaceBook.com).
The program is less than 170 lines of code. You can try it if you are registered at vkontakte.ru, but the program doesn't matter now.
I am very pleased with simplicity of creation of portable rich GUI applications with PyQt, but as always there are some issues on Windows. I created the program on my favorite old Linux box with vim, so there I had no problems with PyQt4 installation and no need of packaging, because the program consists of one file and three icons.
Then I decided to try it on Windows. First of all I installed newest PyQt 4.3.0. This new version is now packed in self-installing archive, which includes Qt and Eric4 IDE, so now you shouldn't have problems which I described in this post.
After that the program worked perfectly, but I needed to create standalone version for people who don't have Python and PyQt on their PCs.
This is rather simple task, but I've spent several hours fighting some issues.
I used py2exe to create standalone executable. The problem was to include PyQt into it. The idea is simple: I needed to specify that PyQt should be included. So I used solution from py2exe.org. I created file called setup.py with following contents:

from distutils.core import setup
import py2exe
setup(windows=[{"script":"tray.py"}], options={"py2exe":{"includes":["sip"]}})

Then I ran python setup.py install py2exe.
But I received errors like "module import failed: _qt". So I decided to mention inclusion explicitly: python setup.py install py2exe --includes=PyQt4,sip. It didn't help, but error message changed to "cannot find QApplication". Then I tried to reorganize imports in my code:

import sys
from PyQt4.Qt import *
from PyQt4 import QtCore, QtGui

gave correct result, and the executable was created.
I believe that this problem with imports is temporary and will be fixed in near future.

So then I used Inno Setup to create setup package and it was really easy.

Conclusion
It is really easy to create portable applications with great GUI with PyQt4.
The only trade off is the size of installed application on Windows: for my 170 lines of code project I've got 5 Mb setup package, which extracts into 17 Mb application.
The reason is simple - lack of common packaging system with dependencies on Windows, so the program needs it's own copy of Python and PyQt.

2 comments:

  1. hola ... i randomly found your site we have similar interests .. math .. computer science... just read some of your articles and wanted to say hi

    im currently building an new app using qt gui also ...

    good luck with stuff
    &
    thanks amigo,
    sergio

    ReplyDelete
  2. Thank you to have published your solution for PyQt. I've had the same problem and that solved it (I know the post is old, but still wanted to thank you, because I'm just learning Python and every bit helps).

    ReplyDelete