Jump to content

Tutorial: Programming using PyQt


Recommended Posts

0. Why PyQt?

When ever one plans the development of an application, an important consideration is the language one will use to code it.

Some of the things that can play a role are:

  1. Will the application have a GUI
  2. Will the application be cross-platform or platform specific.
  3. Do you want a high-level or low-level language (or something in between)

Nowadays, it is very common to want to develop applications that have a user friendly GUI.

For windows only applications C# is a great option. Visual C# Express is available for free from Microsoft and has an easy to use GUI builder.

However, if one wants to code a cross-platform application C# is no longer such an attractive option. While The Mono Project aims to allow for C# applications to be cross-platform there are many things missing.

C++, a very popular and poweful language, is a cross platform language, but unfortunately it does not have a standard GUI builder. What one must do is decide on a package to use for the GUIs. Some populat options are WxWidgets and Qt. However, C++ is a low-level language.

If one prefers a high-level language to stay away from all the complications of programming in C++, Python is a great option. Like C++, it is a cross-platform language. Also like C++, it does not have a standard GUI package.

Because Qt is one of the more powerful and expansive GUI packages for C++ and has been ported to python, PyQt is one of the best options for developing GUI applications with Python.

The official PyQt introduction can be found here: http://www.riverbankcomputing.co.uk/software/pyqt/intro

Or particulat interest is:

Why PyQt?

PyQt brings together the Qt C++ cross-platform application framework and the cross-platform interpreted language Python.

Qt is more than a GUI toolkit. It includes abstractions of network sockets, threads, Unicode, regular expressions, SQL databases, SVG, OpenGL, XML, a fully functional web browser, a help system, a multimedia framework, as well as a rich collection of GUI widgets.

Qt classes employ a signal/slot mechanism for communicating between objects that is type safe but loosely coupled making it easy to create re-usable software components.

Qt also includes Qt Designer, a graphical user interface designer. PyQt is able to generate Python code from Qt Designer. It is also possible to add new GUI controls written in Python to Qt Designer.

Python is a simple but powerful object-orientated language. Its simplicity makes it easy to learn, but its power means that large and complex applications can be created. Its interpreted nature means that Python programmers are very productive because there is no edit/compile/link/run development cycle.

Much of Python's power comes from its comprehensive set of extension modules providing a wide variety of functions including HTTP servers, XML parsers, database access, data compression tools and, of course, graphical user interfaces. Extension modules are usually implemented in either Python, C or C++. Using tools such as SIP it is relatively straight forward to create an extension module that encapsulates an existing C or C++ library. Used in this way, Python can then become the glue to create new applications from established libraries.

PyQt combines all the advantages of Qt and Python. A programmer has all the power of Qt, but is able to exploit it with the simplicity of Python.

For more details, including some simple programming examples, you can download a copy of the PyQt Whitepaper.

As mentioned in the official Why PyQt?, Python is an interpreted language, whereas C++ is a compiled language. This can mean slower applications. When speed is a crucial issue, one might choose C++ despite having to deal with the low-level language implications.

I. Getting Started

Here a few things that you need to get started with PyQt.

Python 2.6


PyQt4 for python 2.6


I started learning pyqt from this book: http://www.qtrac.eu/pyqtbook.html

Even if you cannot buy the book or find it at a library it is still useful to get this:


Inside you will find sample code and more importantly Make PyQt (makepyqt.pyw)

Make PyQt is a python program that will turn the ui files made using the Qt Designer.


While it is possible to develop pyqt programs with only a text editor, there are many conveniences that once can take advantage of. I would at least recommend using the Qt Designer (comes with PyQt) and Make PyQt (makepyqt.pyw) to generate the code needed for the GUIs. Combined with something like Programmer's Notepad this works very nicely.

However, for those who prefer to use an IDE, there is a good option.

The Eric4 IDE is a great tool that helps keep one's projects in order. I will be using it for the examples.

III. Example (Updated example, comming soon)

PyQt programs begin as follows:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ui_ppremain

PyQt4.QtCore and PyQt4.QtGui are the basic modules that we need to make PyQt Programs.

import ui_ppremain, imports the file ui_ppremain.py, which is what makepyqt.pyw outputs when you choose to build in the directory where you have your ui files.

and the code that executes the program as follows:

app = QApplication(sys.argv)
mw = MainWindow()

the code for the main window would be:

class MainWindow(QMainWindow, ui_ppremain.Ui_MainWindow):
   def __init__(self,parent=None):

   def updateUi(self):
       self.nameLabel.text="ROM Name:"

One thing to notice is that indentation is important in python. Structure is provided through indentation rather than braces as in other languages. More concrete examples to come later. Feel free to ask questions.

Edited by SCV
Link to comment
Share on other sites

  • 2 weeks later...
Maybe you could explain or give a link detailing why Python is a good language to learn and what it is best used for?

I second this motion, as I am going to steel myself to break away from VB.Net this summer, and it's basically coming down to C#, C++, or Python. I would like to know which language will suit which projects best.

Link to comment
Share on other sites

  • 2 weeks later...
I second this motion, as I am going to steel myself to break away from VB.Net this summer, and it's basically coming down to C#, C++, or Python. I would like to know which language will suit which projects best.

My daughter is just starting to look into programming. We picked up a copy of Beginning Programming All-In-One Desk Reference For Dummies and they cover the different languages and how they are associated with other languages. The do mention Basic (VB Basic as well) but say that if you learn C++, you can move to almost any other language that is out there. Not only that, you now can work on different kinds of programmable hardware like Atmel's AVRs or embedded processors.

GCC is a free and recommended compiler.

The one point is if you know C/C++, you can get a job that pays more than many other languages. I know that most of the people at work that have to program are using either C or C++. Some are moving to QT due to cross compilers. Of course, GCC supports various OS's as well and can be used as a cross compiler.

I learned assembly many years ago and now I am trying to learn C/C++ to work with microcontrollers.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...