GTK Programming

I found a lot of confusion when starting GTK programming, so I put my experiences here. One problem was to do with versions of Glade compared to versions of GTK. The version numbers don't match up; some versions of glade apply to both gtk 2 and gtk 3; many tutorials don't tell you whether the sample code is gtk 2 or 3; there's disagreement over whether gtk 3 is preferable; the list goes on.

I have chosen to develop in GTK 2, using C as my choice of programming language. This choice was based on the first stable working environment that I was able to create.

The Glade GUI allows you to design a screen layout, with widgets such as text entry fields and buttons.


To start the Glade programming environment, you need package glade-gtk2 and it's dependencies. Install these from your package manager. In Linux Mint 17, the package manager is 'aptitude'.

To compile a C program with GTK 2, you need to install package libgtk2.0-dev and it's (many) dependencies


When you save your glade GUI, ie. the screen layout that you want, it's stored in an XML file,

You then write a program that reads the XML file (to position the fields on the screen), and you write the code to control the user interaction with the screen fields.

Many people seem to use python to write the code; I'm a C programmer, so I chose to use C.

Lesson 1 : Simple text field and the structure of the code

Lesson 2 : Designing a Services Manager

Download the code below for a complete working example of reading and setting text entry, text view and tree view.

Managing services on Linux can be a messy learning curve, which ideally users shouldn't have to go through. I've seen a few service managers come and go, and none of them have ever covered my requirements.

What I want to do is decide whether I need to service to start when I boot my computer; I then want the option to start and stop each service when I choose.

The current offering from Linux Mint 17, package jobs-admin, is another bit of a mish mash. If you install a MySQL database, which is started on boot by default, then you can't control it with jobs-admin. jobs-admin includes control over run-levels, which home users shouldn't need to care about. There's also no descriptive text or advice for the jobs that are included in jobs-admin.

So, as an exercise in learning GTK programming, I chose to start designing a service manager. I ended up with a neat template for GTK C programs, that I wanted to share. Experience with programming is assumed in this tutorial; rather, the emphasis is on where to declare global or local variables, and the basic GTK functions to use.

You can download the code here.