Simple GUI application with GTK+


In this post you will see what steps are required to create a simple graphical application under gnu/linux. For this purpose  we will use GTK+. If you have never heard about it, it’s a big collections of libraries behind GNOME.
source code of “simple.c”

/* First, we need to import the gtk+ library set. */
#include <gtk/gtk.h>

struct _Widgets
  GtkWidget *window, *hbox, *entry, *button, *vbox, *label;

typedef struct _Widgets Widgets;

static void button_cb( GtkButton *, Widgets * );

int main( int argc, char **argv )
  /* We declair pointers to the widgets. */
  Widgets *widgets;

  /* We need to call this initialization function before we can do any
   * other function calls. */
  gtk_init( &argc, &argv );

  widgets = g_slice_new( Widgets );

  /* First, an empty window is created. */
  widgets->window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
  gtk_window_set_title( GTK_WINDOW( widgets->window), "A Simple Example" );
  gtk_widget_set_size_request( widgets->window, 250, -1 );

  g_signal_connect( widgets->window, "destroy",
		    G_CALLBACK( gtk_main_quit ), NULL );

  widgets->entry = gtk_entry_new( );
  widgets->label = gtk_label_new( NULL );
  widgets->button = gtk_button_new_with_label( "click me!" );

  g_signal_connect( widgets->button, "clicked",
		    G_CALLBACK( button_cb ), widgets );

  widgets->hbox = gtk_hbox_new( FALSE, 0 );
  gtk_box_pack_start_defaults( GTK_BOX( widgets->hbox ), widgets->entry );
  gtk_box_pack_start( GTK_BOX( widgets->hbox ), widgets->button,
		      FALSE, FALSE, 0 );

  widgets->vbox = gtk_vbox_new( FALSE, 0 );
  gtk_box_pack_start( GTK_BOX( widgets->vbox ), widgets->hbox,
		      FALSE, FALSE, 0 );
  gtk_box_pack_start( GTK_BOX( widgets->vbox ), widgets->label,
		      TRUE, TRUE, 0 );

  gtk_container_add( GTK_CONTAINER( widgets->window ), widgets->vbox );

  gtk_widget_show_all( widgets->window );

  gtk_main( );

  return 0;

static void button_cb( GtkButton *button, Widgets *widgets )
  gtk_label_set_text( GTK_LABEL( widgets->label ), "clicked" );
$ # it's time to compile simple.c
$ gcc -o simple simple.c $( pkg-config --libs --cflags gtk+-2.0 )
$ ./simple

3 thoughts on “Simple GUI application with GTK+

  1. You’ve allocated memory using g_slice_new() but you haven’t freed it. You need to free it by adding g_slice_free (Widgets, widgets); between gtk_main(); and return(0);
    Also you’re using GTK+2 which is quite old now. I would suggest switching to GTK+3.

    • Hello Mark, thank you for your comment.
      In general you are right, there is a golden rule like: “Every time you use malloc, call free.” However and specially for this trivial example it is absolutely not necessary to place g_slice_free call between gtk_main and return 0, as this allocated memory is required during the whole application life time and once the main loop is closed the total allocated memory of this sample app, here, is freed by the kernel.

      About gtk+2. Well, this post is almost one year old and I have several posts targeting gtk+3 API published already. It would be a pleasure for me, if you will find it useful for you. Nevertheless, it should be mentioned that gtk+2 is not old. The gtk+ 2.24.14 has been just released 6 days ago. For the end user gtk+3 does not bring many significant features and can be seen as major update named 2.26 with a huge API breakage, followed by a developer headache. So many big projects like Xfce, for example, still count on gtk+2!

      For the other side it can not be a bad decision to go with gtk+3 for a newly created project.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s