This article is an introduction to the PyQt GUI Application Development Framework that uses Python and Nokia’s Qt.
Contents:
Pretext
Ever wondered how to easily write good programs using Python? Programs that would run on any platform – from Windows Vista to Mac OS X along with the various Linux variants? Programs that look good, look native, and also work great?
Here’s one solution – PyQt! With it you can create complete, working applications that may look like this:

Nokia’s Qt is a free (LGPL) cross-platform C++/Java based application-framework, with GUI being its most prominent feature.
PyQt is simply a python-binding to the C++ libraries that Qt provides. Using PyQt one can write neat looking GUI applications that do a lot, and do so easily.

A series of articles, starting with this one will aim to tutor interested programmers into using this wonderful library, and developing applications with it.
Prerequisites
The most-minimal pre-requisites are:
- Basic Python programming knowledge.
- A little know-how about GUI and the event-based-programming concepts.
Two primary resources where we start from:
Downloading and installing PyQt is fairly easy on Linux thanks to the various package managers. Do check out the download links at each of the above-mentioned links to get them on other platforms as well. Installation help is also provided by Riverbank Computing to help you set-up the PyQt4 library on your OS.
Sample Code
So let’s start off in the same style as almost everything do in a programmer’s perspective: Hello World!
Below is a well documented program for the same:
import sys # Importing the necessary Qt classes. from PyQt4.QtGui import QLabel, QApplication # We use the from foo import * syntax here because # all of Qt's objects begin with a Q # and thus we shouldn't run into namespace problems. if __name__=='__main__': App = QApplication(sys.argv) # All Qt programs need an # QApplication instance. # We pass the sys.argv as its arguments # because Qt is adept at handling some # of the default command-line options # like style, size, etc by itself. Label = QLabel( "Hello World!" ) # QLabel is the class providing a # simple label Label.show() # Like in most GUI toolkits, we have # to manually set it to show App.exec_() # Notice the _ after exec, this is to # avoid the confusion with Python's # exec() built-in-function # exec_() starts the main application # loop. Something like main() of other # toolkits.
This is how it looks when run, a simple application that displays some popular text:
![]()
Breaking it down
The first thing we have initialized under our ‘__main__’ is the QApplication instance, App. Every GUI application needs this instance to be present. It handles the look and feel of the application and is the main thread of the same. Without it Qt will error out and not run any GUI.
Next we create a label. A label is a simple text-displaying widget used in many forms to describe the input fields and other things. In Qt, its called the QLabel and resides under the QtGui namespace. We create one with a simple Hello World text under the object name Label.
Now we call the show() method of the label so that it appears on the screen. This method is applicable to all QWidget-class children, and QLabel is one among them. Calling show() draws the label widget’s window on the screen.
Finally, we start the application loop, the one responsible for making an application run as an infinite loop waiting for user-interaction. Its done by the exec_() method of QApplication (App is the instance name). Note the _underscore_ in the function name, this is to avoid confusion between Python’s own exec() built-in-function.
Getting Further
Tutorials
- Learn Basics of PyQt
- Learn the basics of what PyQt is entirely about.
- Learn how to use the fantastic Signal and Slot system of PyQt
- Begin programming with event based concepts – Signals and slots. And learn to use Layouts to organize your GUI.
- Learn how to design GUI interfaces visually using Qt Designer
- Create your GUI Forms and Windows using a Visual Designer supplied by PyQt. Designing Windows at its simplest.
FAQs
- PyQt FAQ on Custom Signals, JPEG Images, Mouse Hovers and More
- Learn to write your own signals for use with slots, save images as JPEG using the QImage PyQt Class and learn to track mouse hover events. Plus some more tips.
Footnotes
Some more thoughts on the article and the code.
- Am running this Qt application in a GNOME environment and thus you see the GTK style being used. Qt can adapt to your environment and can use the native style of any platform it runs on. Also, you can try out the -style option to change it too. More on this as we progress.
- This program is just a quick start for the impatient, a dose for the hyperactive. I shall take a slow, but smooth, approach to the finer details of PyQt as the tutorial progresses.
Coding style looks just like C and VB. Should give it a shot.
Joseph
26 Apr 09 at 3:40 pm
Do give it a shot, the articles shall be regular
Harsh
26 Apr 09 at 4:17 pm
Nice, looking forward to the series.
Sathya
27 Apr 09 at 3:08 am
Great then!
Joseph
27 Apr 09 at 10:53 am
Hello
I am learning Python and will move to PyQt for GUI building, i think PyQt is much more powerful and easier than wxPython.
Can you please continue to release more tutorials especially creating a small useful program from scratch
I looked into wxPython, but did not like it. Can you continue to post more tutorials on PyQt.It would be very helpful if the tutorials are geared towards creating small usefull applications. Some plain application, some using SQL Database, some using Web Services etc.
Ill be keeping an eye on this page.
Ibn Saeed
27 Apr 09 at 7:54 pm
Hello and thanks for dropping by!
Sorry about the comments not appearing earlier, Akismet was having a hiccup I believe.
I’ll be sure to take a nice approach to PyQt, and you can also check out the PyQt Demos that accompany the whole package!
Harsh
27 Apr 09 at 9:57 pm
Lookin forward to more great articles. I’m loving oython so far and will soon start learning PyQt.
P.S. Good to see some of my icons after a long time
abhinandh
28 Apr 09 at 3:54 pm
[...] using PyQt but are interested in great cross-platform GUI application development please read the PyQt Introduction [...]
PyQt – Creating interfaces visually with Designer at Harsh J
18 Apr 10 at 9:31 pm
[...] using PyQt but are interested in great cross-platform GUI application development please read the PyQt Introduction [...]
PyQt – Signals, Slots and Layouts Tutorial at Harsh J
18 Apr 10 at 9:32 pm