9.2 - Creating GUIs with GTK

9.2 - Creating GUIs with GTK

GTK (GIMP Toolkit) is a powerful, open-source toolkit for creating graphical user interfaces (GUIs). It is built on top of GLib and used by applications like GIMP, Inkscape or Gnumeric.

GTK uses an object-oriented programming model in C through a system called GObject, and it integrates tightly with GLib for event loops, signals, and utilities.

First GTK Program: A Simple Window

#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
    gtk_init(&argc, &argv);

    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Hello, GTK!");
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);

    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

Step-by-Step Breakdown

1. #include <gtk/gtk.h>

This includes the complete GTK+ header.

  • Provides access to widgets like GtkWindow, GtkButton, GtkLabel, and layout containers.
  • Also includes GLib and GObject headers automatically.
  • 2. gtk_init(&argc, &argv);

Initializes GTK and parses any GTK-specific command-line options.

  • You must call this before using any GTK functions.
  • It modifies argc/argv to remove GTK-related flags.

3. Creating a Window

GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  • Creates a new top-level application window.
  • GtkWidget is a generic pointer for all GTK widgets.

4. Setting Window Properties

gtk_window_set_title(GTK_WINDOW(window), "Hello, GTK!");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
  • Sets the window title and default size (in pixels).
  • GTK_WINDOW() is a macro that casts to GtkWindow* safely.
  • 5. Handling Events (Signals)

g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  • GTK uses a signal system to handle events.
  • "destroy" is emitted when the window is closed.
  • gtk_main_quit stops the GTK event loop, ending the application.

6. Displaying Widgets

gtk_widget_show_all(window);
  • Makes the window (and any contained widgets) visible.

7. Entering the Main Loop

gtk_main();
  • Starts the GTK event loop, which waits for and dispatches events (keyboard, mouse, etc.).
  • Your program stays here until gtk_main_quit() is called.

Compilation with GTK

Use pkg-config with GTK for proper flags and linking:

gcc gtk_example.c $(pkg-config --cflags --libs gtk4) -o gtk_example

If you’re using GTK 3 instead of GTK 4, replace gtk4 with gtk+-3.0:

gcc gtk_example.c $(pkg-config --cflags --libs gtk+-3.0) -o gtk_example

Output

Running the program will show a basic empty window titled “Hello, GTK!”.

GTK and GLib

Feature Provided By
Data types, memory, I/O GLib
Objects, signals GObject
GUI widgets, layout GTK

GTK depends on both GLib and GObject, so understanding them is helpful for larger applications.

More information

You can refer to https://docs.gtk.org/ to see more information about using Glib and GTK


Summary

GTK allows you to build native desktop applications in C with windows, buttons, and layouts. It builds on top of GLib and GObject, giving you a full-featured and portable GUI toolkit using C.



© 2025 Easy and fast Programming guide For C

Powered by Tessera for Jekyll