PyGTK For GUI Programming/Introduction

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 (the Application Programming Interface) is itself 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 user interface windows and 'widgets' - the term for items inside the window (for example a push button or a text input box) - and linking program functions to the signals generated by the interface. 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 supplement your knowledge.

What is a GUI?

edit

The way in which users interact with programmes has been revolutionized with the window-based, mouse-driven desktops in modern operating systems, creating greater accessibility and ease of use by the simple 'point-and-click' approach. A GUI is a window and its collection of widgets - those input boxes, check buttons, radio selections, tabs, text, buttons, and other objects placed inside the window. When the user does almost anything with the window or one of the widgets, even just running the mouse pointer over it, the GUI sends a signal to the programme to alert it that a response may be required. The Python programming language includes a module for creating basic GUIs in its standard set of libraries, it's named Tkinter, although some don't consider it very elegant in its appearance, nor feature-rich enough in its capabilities. The GTK+ graphical library is a useful alternative. Whether you intend to write a script to allow the user to perform a simple task 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 interpreter (currently v2.6 or later) installed - if not, see the tutorials above.

Linux

edit

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 ftp.gnome.org, 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.

Windows

edit

1- Delete any old or partial installations before installing the new packages
2- Install Python using the windows installer. Example: python-2.7.1.msi
3- Install the "all-in-one" package using the windows installer. Example: pygtk-all-in-one-2.22.5.win32-py2.7.msi
That's it. You're done.

The following original instructions did not work but the above procedure from a user group did the job. I have left them here so the author can revise them.
The best way to install PyGTK on windows is to download the GTK and PyGTK binaries. First, download `gtk+-bundle_2.18.7-20100213_win32.zip` from ftp.gnome.org, 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 ftp.gnome.org, 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.

PyGTK For GUI Programming
 ← [[PyGTK For GUI Programming/|]] Introduction First Steps →