C, GTK

Simple ui using GTK and C

Let’s create a simple GTK window that is centered on screen.

1. Create a folder in your home directory and name it for ex. gtk_window.
2. Create a file, name it for ex. app.c and paste the following code:

#include <gtk/gtk.h>
 
int main( int   argc,
          char *argv[] )
{
    //initialize
    GtkWidget *window;
    gtk_init (&argc, &argv);
 
    //create window
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
    //exit application when x is clicked
    g_signal_connect_swapped(G_OBJECT(window), "destroy",
      G_CALLBACK(gtk_main_quit), NULL);
 
    //set window title bar
    gtk_window_set_title(GTK_WINDOW(window), "Simple window!");
 
    //set window size
    gtk_window_set_default_size(GTK_WINDOW(window), 500, 250);
 
    //set window position (screen center)
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
    //show window
    gtk_widget_show  (window);
 
    //main processing loop
    gtk_main ();
 
    return 0;
}

3. Open terminal and change directory to gtk_window folder.
for ex.

cd /home/myusername/dev/gtk_window

4. Issue the following command to compile the application:

gcc -Wall -g app.c -o app `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`

5. Run the application:

./app