This is meant to be a comprehensive guide on how to get started coding a cross platform GUI application using python.
For achieving this we will use Eclipse with the plug-in PyDev and PyQT which is python bindings for the cross platform UI framework QT. My aim is to make this guide as thorough as possible so if you already know some of the following parts please just skip them.
If anything is missing or you find something unclear please leave a comment and I will correct it.
The outline of we are going to do is:
Download and install Python
First you need python installed on your computer. I suggest you download Python 2.6.5 because it is widely supported. For Windows users, use the windows installer suitable for your system. They will suggest you install Python in c:\python26 and I have done so aswell. Usually I don’t like cluttering my c:\ with catalogs but for this I make an exception.
After install is done it is nice to make python available in the command prompt regardless of the catalog you are using. Do this by adding “;c:\python26″ to PATH in your environment variables. You find your environment variables by right clicking on “My computer”, select properties and go to advanced settings.
You should now be able to go to your command prompt and type python to start python. This is used when you are installing new libraries and trying out python commands.
Download and install PyQt4
PyQt is a set of Python bindings for Qt. It is needed to make Qt classes available in Python. For more info on PyQt check out the What is PyQt section at Riverbank Computing.
Update – Install SIP
As Will Stich (thanks again Will!) commented below you first need to download and install SIP to get PyQt to work . Do this by downloading SIP from the Riverbanks Computing download page. In this tutorial we will use sip-4.10.5.zip.
Extract the contents of the zip where ever you please. I will put it in c:\sip-4.10.5 in this example.
When that is done you can open the command prompt (run->cmd) and go to your SIP folder (c:\sip-4.10.5). Type “c:\python26\python.exe configure.py” and SIP will be configured.
Installing PyQt is pretty straight forward. Go to the PyQt4 download page and download the windows installer for the PyQt version that corresponds to your python version you are using. In this example we use PyQt-Py2.6-gpl-4.7.3-2.exe.
The windows installer will installer should do all the work for you so if you installed Python in C:\Python26 you should be fine by just pressing next all through the installer. When done your PyQt4 installation should be saved in C:\Python26\Lib\site-packages\PyQt4.
Download and install Eclipse
Eclipse installation is pretty straight forward. I chose Eclipse IDE for Java developers but I think any version works fine for our purpose. Unpack into program files or where you wherever. Heck you could probably put it on a USB stick if you want to.
Download and install Pydev
Pydev is a great plugin for Eclipse that allows python syntax highlighting and code completion. It can be installed in two ways; either you download and unpack the files into the plug-in folder in your Eclipse directory or you use the installation manager in Eclipse. I suggest you use the installation manager!
So, start eclipse, choose a workspace you want to use and go to “Help -> Install new software…”. Press “Add…” and enter a name of the plugin (Pydev) and the pydev plugin update URL: http://pydev.org/updates/ (the url might change so if it doesn’t work, go to the pydev site). Follow the steps in the installation guide. You might get a bunch of dialog boxes warning you about stuff, I just pressed yes all the way and it worked fine.
When that is done we can create our first Python project with Pydev. Your “File -> New” menu should look like the image below. Select “Pydev Project”.
A new dialog box will open. Write the name of your project in the “Project name:” field. If this is the first time you create a Python project you will need to configure your Python interpreter. Do so by clicking on the “Click here to configure an interpreter not listed” link. A new dialog box will open that looks like the one in the image below.
Press the “Auto Config” box and Pydev will (hopefully) find your installed python interpreter. The auto config dialog findings should open in a new dialog that looks like the one below. Select the ones selected in the figure below and click “OK”.
Now this might be over pedagogical but your Python Interpreters box should look like the figure below. Press OK we are done with the interpreters!
Now press “Finish” to create your Pydev project. Eclipse and Pydev will create a src folder and show the referenced Python interpreter.
Getting code completion for PyQT in Eclipse
Not having code completion while learning something new or using a new library is a pain so lets try to avoid that! To get code completion for your PyQT library we need to add it to the external libraries included in the project. Go to the menu Project -> Properties -> PyDev PYTHONPATH -> External Libraries -> Add source folder. Add the path where you have installed your PyQT files. They are most likely, if you used the windows installer, locaded in C:\Python26\Lib\site-packages\PyQt4. If all is well you should have something that looks like the figure below.
Creating files and coding a tiny PyQT GUI
Now we are done with the development environment things! Lets try if this thing works! First thing we need to do is to add a main module. Do this by right clicking on your src-folder select new -> Pydev Module. In the “Name” field enter “Main” and in the template. You could name the file anything you like, it will work either way, but it is in my opinion always nice to have a startup file called Main. It should look like the image below.
This will create a new main module for you with the code below
''' Created on 2 apr 2010 @author: Mikael Halén ''' if __name__ == '__main__': pass
Now, since this just is a small example I will put all the code in this file. If you would to start a bigger project you would probably want to create a new file for your user interface.
Anyway, we want to create a QT GUI so lets start doing that! I’m going to describe this process by first showing you the end result and then I’m going to describe what the different rows do afterwords. Here is what we will end up with:
And this is the code you need to get it working:
from PyQt4 import QtGui class HelloPython(QtGui.QWidget): def __init__(self, parent=None): super(HelloPython, self).__init__(parent) helloLabel = QtGui.QLabel("Say Hello To PyQT!") helloLineEdit = QtGui.QLineEdit() mainLayout = QtGui.QGridLayout() mainLayout.addWidget(helloLabel, 0, 0) mainLayout.addWidget(helloLineEdit, 0, 1) self.setLayout(mainLayout) self.setWindowTitle("My Python App") if __name__ == '__main__': import sys app = QtGui.QApplication(sys.argv) helloPythonWidget = HelloPython() helloPythonWidget.show() sys.exit(app.exec_())
Now lets see what that means. Lets start from the top!
from PyQt4 import QtGui
This imports the Qt libraries needed for this little application. We only needed the QtGui so that is what we imported.
This is your Main class. This line creates the class called HelloPython and says that it should inherit from QtWidget which is the base Qt class for all user interface objects.
def __init__(self, parent=None): super(HelloPython, self).__init__(parent)
This is the class constructor and it will be called when you instantiate the object. These two rows will make this class have no parent and thus making it the main class.
helloLabel = QtGui.QLabel("Say Hello To PyQT!") helloLineEdit = QtGui.QLineEdit() mainLayout = QtGui.QGridLayout() mainLayout.addWidget(helloLabel, 0, 0) mainLayout.addWidget(helloLineEdit, 0, 1) self.setLayout(mainLayout) self.setWindowTitle("My Python App")
These lines are QT specific. First one Label is instantiated for displaying text and then one LineEdit for entering text in. Then a GridLayout is set up where we put the Label and LineEdit. Lastly we set the layout in on the QWidget and set the window title.
if __name__ == '__main__':
This is the main python module and this line of code makes this a standalone application.
sys is a necessary import for sys.exit(app.exec_()) later in this file.
app = QtGui.QApplication(sys.argv)
A QT application must have a application object. The sys.argv are command line arguments passed on to the application.
helloPythonWidget = HelloPython() helloPythonWidget.show()
Instantiation of the QtWidget and displaying it for the user.
Here we enter the main loop and all events are handled. On exit() the loop ends and sys.exit() makes sure the exit of the application is handled correctly by the system.
With this guide I hope you can easily get started programming GUI applications in Python. Again, if you find that something is unclear, missing or plain wrong please leave a comment or contact us in some other way and we will fix it. I will continue posting solutions I use for the problems I encounter while developing in QT so please subscribe to our RSS feed or follow us on twitter.
If you want to read more visit the learning python blog or read zetcode’s python tutorials. You should also visit the PyQT class reference library at www.riverbankcomputing.co.uk.
5 May 2010
There is a great set of tutorials (and a link to this post) that you can use for further learning at the diotavelli PyQTWiki