PyQt
Page updated: 15 Sep 2008
This page is about PyQt, which is a set of bindings to allow using Python with TrollTech QT
The story so far
- I seek a way to design GUI interfaces for Python.
- I want one that runs cross-platform (Windows and Linux)
- I want to create apps that can run on my OpenMoko FreeRunner.
I want to be able to design the user interface with a wysiwyg tool. Qt provides that in the form of Qt4 Designer. Then I want to be able to get access to that UI from Python. With QT, you compile the UI (which is an XML file) into Python code, and then load it as a module. You connect your own code to the UI using events and event handlers called signals and slots.
As a test I am going to develop a GPS data collection application that is portable between Windows XP, Ubuntu Linux, and Debian Linux on the OpenMoko Neo FreeRunner smartphone.
So far I am successfully using ActiveState Komodo as the Python IDE and QT4 Designer to design the user interface.
I have only worked my way through some tutorials, not done anything useful yet. I am ready to start writing real programs now.
Articles and references
At DevShed: PyQT Getting Started
Some tutorials:
Creating the UI programmatically: http://zetcode.com/tutorials/pyqt4/
PyQt 3 (outdated): http://www.cs.usfca.edu/~afedosov/qttut/
The official documentation: http://www.riverbankcomputing.com/static/Docs/PyQt4/pyqt4ref.html
Book review: Summerfield, Mark: Rapid GUI Programming with Python and Qt, 2008
I got it last week so the jury is still out but it seems useful. It's a textbook. The first 1/3 is a Python tutorial (I was hoping for more Qt and less Python but it's a textbook so it makes sense in a classroom context.)
Development tools
I use ActiveState Komodo as my Python IDE. I recommend it highly even though it costs money. There is a CIX package for QT4 available from ActiveState. (You install the CIX package to add code completion features for a language or in this case the QT4 bindings for python.)
Windows A complete Windows binary package for QT4 is available from http://www.riverbankcomputing.co.uk/pyqt It includes everything you need, including the runtime, the designer, the bindings. Quite nice.
Linux For development on Ubuntu, you just add the QT4 packages from Synaptic package manager. Even easier than Windows. I have not tried to run anything on the smartphone yet so I don't know what that will entail. I only need runtimes over there. I don't plan on programming on the phone though it should be entirely feasible, at least for the Python parts.
Development cycle
1 Design a user interface in QT4 Designer. Save as a .ui file (XML).
2 Generate Python code from the UI file using pyuic.
pyuic4.bat -x myForm.ui > myForm.py On Windows it's bat file pyuic4 -x myForm.ui > myForm.py On Linux it's a script
3 Test the UI. The '-x' option to pyuic causes it to emit a built-in test program so you can just run myForm.py from the command line to test it.
4 Create a python wrapper with the event loop in it. For example, this is similar to what the '-x' option gives you.
import sys from PyQt4 import QtGui from myForm import Ui_MainWindow app = QtGui.QApplication(sys.argv) window = QtGui.QMainWindow() ui = Ui_MainWindow() ui.setupUi(window) window.show() sys.exit(app.exec_())
This suffices if you don't need to call any Python code from your user interface. (I suppose that could happen somehow?) This example shows how to call code from buttons.
#!/usr/bin/env python import sys from PyQt4 import QtCore, QtGui from DispatchCenter import Ui_MainWindow class MainWindow(QtGui.QMainWindow): def __init__(self): QtGui.QDialog.__init__(self) # Set up the user interface from Designer. self.ui = Ui_MainWindow() self.ui.setupUi(self) #self.tableView # Connect up the buttons. self.connect(self.ui.btnSend, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("sendDispatch()")) self.connect(self.ui.btnCustom, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("openCustomForm()")) def sendDispatch(): # do something here pass def openCustomForm(): # do something here pass app = QtGui.QApplication(sys.argv) window = MainWindow() ui = Ui_MainWindow() ui.setupUi(window) window.show() sys.exit(app.exec_())