Programming with PyGTK and Glade

This is my translation of Florian Diesch’s guide found here: http://www.florian-diesch.de/doc/python-und-glade/online/einleitung.html

I translated this guide as there doesn’t seem to be anything about using gtkbuilder and glade, rather than the older libGlade, with python. The guide is not yet complete, but I will keep working on it over the coming weeks.

Dieser Werk bzw. Inhalt ist unter einer Creative Commons-Lizenz lizenziert. This work is released under a creative commons licence (cc-by-nc-sa).

Introduction

This guide should give you a first look at how to use the programming language Python and the interface designer Glade.

I will concentrate on the Python code rather than the use of Glade, which is already well documented in the Glade Interface Designer Handbook.

This text neither should, nor can be used as a replacement for the PyGTK Reference Manual, or the offical PyGTK Tutorial.

I will take it as a given that you have experience with Python and that you are working with Glade 3.6.x on Linux and that your project is saved in the GTKBuilder format for GTK 2.16.

Signals and Events

Like many libraries for graphical user interfaces, GTK is event driven. A central event loop waits until something happens then forwards the control onto the routines responsible for the processing of the event.

In order to process events, GTK uses signals: control elements and other objects react to events, which the corresponding signals trigger. Using these signals, you can control callbacks, which will then always be called, so that the corresponding signal will be triggered.

Note:
It is conventional to name callbacks ‘on_OBJECTNAME_SIGNALNAME’ E.g. the ‘pressed’ signal of the object bt_ok (an ok button) would be ‘on_bt_ok_pressed’.

Signals make the connection between the user interface and the program’s code such that: every action of the user triggers one or more signals, which you can connect to with callbacks, in order to react to the user’s request. In addition, signals are often used to enable different parts of the program to communicate with one another. Therefore, a GTK program often contains very many signal callback routines.

Connecting to Signals

Using the method gtk.Widget.connect() you can connect a callback with a signal.

Instead of this, you can use gtk.Builder.connect_signals() to automatically connect all callbacks with their corresponding signals.

Additionally, you must specify the name of the callback in the signal properties of the gui element in Glade.

Whilst a callback is being called, the main loop cannot process any signals. Therefore, the interface doesn’t react to the mouse, or the keyboard and also, changes to the interface elements are usually seen only after completion of the routine.

Therefore, in a callback which is activated by the keyboard/mouse, it is often useful to trigger a handler, for example a progress bar, using the following lines of code:

while gtk.events_pending():
      gtk.main_iteration ()

Important Functions

gtk.main()

Starts the main loop.

This function is normally called after you have shown the main window of your program.

gtk.main_quit()

This function ends the main loop. It is usually called in the callback for the delete-even signal of your main window.

gtk.events_pending()

returns: True if an event is waiting to be processed, otherwise False

Checks whether there is currently an event waiting to be processed.

gtk.main_iteration(block=True)

Parameter: block – True, if the function should wait an event has been processed.

Executes a single iteration of the main loop.

This function is typically called together with

gtk.events_pending()

in order to execute all queued events.

Example

def on_window1_delete_event(self, *args):
     gtk.main_quit()

This is the on_window1_delete_event, the callback for the delete-event signal of window “window1”. Should this window be destroyed (closed), then gtk.main_quit() will be called, so as to end the main loop and with that, the program itself.