Gtk+ programing tutorial for beginner

First program for GTK+ beginner is just make a window widget and show on screen. GTK+ is graphical user interface library toolkit. It is a cross platform library which offers complete set of widgets and controls for creating a native user interface. It is also called GIMP toolkit and backbone of gnome desktop. gnome desktop is a package which provides desktop environment for linux based operating system like ubuntu, fedora, centos and many more. Now a days the light version of gnome desktop is used in smartphone os, the light version of gnome desktop is know as mobile gnome desktop.

For a gtk+ beginner programer this article will boost the starting interest. now let us create our first program and then we will see how to compile, run the program. And after running our first gui we will see “How it works”.

(1) Save the below code as mywindow.c

(2) To compile mywindow.c , run below command on terminal.
$ gcc mywindow.c pkg-config --cflags --libs gtk+-3.0 -o mywindow

Note: if you have not installed gtk library . Then from your terminal you can type below command to install gtk+ library on debian, ubuntu.

$ sudo apt-get install libgtk-3-dev

For other linux based os you can install gtk+  library from their respective package manager like for redHat or centos you can install by rmp package manager.

If your os is somewhat old then you can install gtk2 library:
$ sudo apt-get install libgtk2.0-dev

In this case your compilation command will be as below

$ gcc mywindow.c pkg-config --cflags --libs gtk+-3.0 -o mywindow

(3) Now just run the output mywindow.


First program for GTK+ beginnerFirst program for GTK+ beginner

This above window will popup on screen. But this is very basic and when you try to close this window ,the window will disappear from the desktop but you will not end up with your terminal.  This means that the program mywindow is still running on your system. Then “How will we quit our program”?

Let us first learn a little about Events, Signals and CallBacks in GTK

GTK+ has its own system of events and event listener. The Event ( like click,destroy ,resize etc ) is know as signals and the function which gets called after emitting these signals is called callbacks. GTk+ signals are emitted by gtk+ widget when user interacts with these widgets. we can connect these signals with a user defined function ( callbaks ).

The connection between a signal and a function ( callback ) is achieved by a gtk+ API.