User:4crickj/PyGTK For GUI Programming/Introduction

From Wikibooks, open books for an open world
Jump to: navigation, search

PyGTK is a module for the Python programming language which allows access to the GTK+ Graphical User Interface (GUI) toolkit. From their website:

"GTK+ is a highly usable, feature rich toolkit for creating graphical user interfaces which boasts cross platform compatibility and an easy to use API."

With the PyGTK binding you can create advanced graphical layouts so that your users are able to interact easily with the application, and the API itself is simple and easy to learn.

This Wikibook aims to guide existing Python programmers who are new to PyGTK and the GTK+ toolkit in using its functions and classes to build windows and 'widgets' - the term for items inside the window (for example a push button or a text input box) - and linking functions to signals. At the end of the book we discuss how the creation of GUIs can be made simpler with the Glade XML library and the GTK Designer.

The reader is expected to have knowledge of the Python programming language. The following books are recommended for beginners:

This book is not a complete reference to the GTK API - that can be found on the Gnome website - nor does it discuss building advanced applications. Beginners to the subject should feel right at home, but if you are already familiar with PyGTK, take a look at the chapters on Glade and Threading, as they may suppliment your knowledge.

What is a GUI?[edit]

The way in which users interact with programmes has been revolutionized with windowed desktops in modern operating systems, creating greater accessibility and ease of use by the simple 'point-and-click' approach. A GUI is a collection of input boxes, check buttons, radio selections, tabs, text and buttons placed inside a window, and sends signals the the programme every time the user does something with it. The Python programming language includes a module for creating basic GUIs in its standard library: this is called Tkinter, although it's neither very elegant in its appearance or feature-rich, so the GTK+ graphical library is a useful alternative. Weather you intend to write a script to allow the user to perform simple tasks such a backing up a few files, or you want to create an advanced RSS reader, creating a GUI is often essential - especially if your intended users may not be familiar with the command line.

GTK+ is the dominant GUI library on the Gnome desktop, so if you are looking to develop applications for the Gnome platform with Python, then knowledge of the PyGTK API is almost certainly necessary.

Installing PyGTK[edit]

We assume you already have the Python 2.6 interpreter installed - if not see the tutorials above.


Ubuntu Users - you already have GTK and PyGTk installed!

Other linux distros - first test if you have PyGTK installed by opening a Python interpreter and typing `import gtk` , if there is an ImportError, then you need to install the module, otherwise it is already installed.

Installing PyGTK

Installing PyGTK from the Synaptic Package Manager.

The easiest way to install PyGTK is to use your system's package manager to download and install `python-gtk2` (see screenshot). This should automatically sort out all GTK dependencies. Once completed, test that it has been installed properly by typing `import gtk` in a Python interpreter - if there are no errors then you're good to go.

An alternative way to install PyGTK on linux is to build it from source. Download `pygtk-2.14.1.tar.gz` from, extract the archive and read the `INSTALL` file for instructions on building. You must also make sure you compile any dependencies, for example GTK, before compiling PyGTK.


The best way to install PyGTK on windows is to download the GTK and PyGTK binaries. First, download `` from, extract and copy to a suitable folder in your system. You will then need to add the `bin` subfolder to the system path.

Then download `pygtk-2.16.0+glade.win32-py2.6.exe` from, and run the installer wizard. Once completed, test that it has been installed properly by typing `import gtk` in a Python interpreter - if there are no errors then you're good to go.