GTK

Also known as: GTK toolkit

On this page 8

GTK is a free and open-source widget toolkit for building graphical user interfaces, written in C and distributed under the GNU LGPL, so that both open-source and proprietary applications may link against it. Among the development tools of programming, it supplies the windows, buttons, text fields, list views and drawing surfaces that a desktop application needs, together with the event loop that drives them.

The name originally stood for the GIMP Toolkit, because GTK was created in the late 1990s for the GIMP image editor. It later became the foundation of the GNOME desktop and many other Linux applications, and the project dropped the old “GTK+” spelling in 2019. The current major series is GTK 4, released at the end of 2020, while GTK 3 remains widely deployed.

How the GTK architecture is layered

GTK is not a single library but a stack of cooperating libraries, each responsible for one layer of the work. The application code talks to widgets, and the lower layers translate those widgets into pixels and input events on a specific platform.

At the bottom sits GLib, which provides data structures, the main event loop and portability helpers. On top of it, GObject adds a runtime object system with classes, properties and signals to a language that has none of these natively. This design is what allows GTK to be used from many languages without rewriting the core.

The following table lists the main components and the responsibility of each one.

ComponentResponsibilityTypical developer contact
GLibEvent loop, containers, utilitiesTimers, idle callbacks, strings
GObjectClasses, properties, signalsSubclassing widgets, connecting signals
GDKWindowing and input abstractionDisplay, clipboard, cursors
GSKScene graph and rendering (GTK 4)Custom drawing via snapshots
PangoText layout and font shapingLabels, rich text, fonts
Cairo2D vector graphicsLegacy and custom drawing code
Main libraries in the GTK stack

GDK backends isolate GTK from the platform: there are backends for X11, Wayland, Windows and macOS, plus the experimental Broadway backend that renders into a web browser. A well-written GTK application therefore compiles on several systems with little or no platform-specific code.

Widgets, signals and the main loop

A GTK program is event-driven: it builds a tree of widgets, connects handlers to their signals and then hands control to the main loop, which waits for input and dispatches events. A button emits a clicked signal, a text entry emits changed, and the application reacts in its callbacks.

Modern applications start from GtkApplication, which handles startup, single-instance behavior, actions and integration with the desktop session. User interfaces are usually described declaratively in GtkBuilder XML files, while the newer Blueprint markup offers a more compact syntax that compiles to the same XML.

A critical rule follows from this design: GTK is not thread-safe. All widget calls must happen on the main thread, and worker threads must pass results back through mechanisms such as g_idle_add or asynchronous GIO operations. Ignoring this rule causes intermittent crashes that are very hard to reproduce.

Appearance is controlled by CSS-like stylesheets, so themes adjust colors, spacing and borders without changing code. In GNOME applications, the companion library libadwaita supplies adaptive widgets and the official design language.

GTK versions and how they differ

Each major GTK version breaks API compatibility, so moving an application between them is a real porting project rather than a recompile. Within a major series, even minor numbers mark stable releases (for example 4.12 or 4.14), and odd numbers are development snapshots.

The table below summarizes the series that developers still meet in practice.

SeriesFirst stable releaseRendering modelCurrent status
GTK 22002Cairo drawing on X11End of life, legacy code only
GTK 32011Cairo, CSS theming, Wayland supportMaintained, no new features
GTK 42020GSK scene graph, GPU renderersActive development
Major GTK release series

GTK 4 moved rendering to the GPU through the GSK scene graph with OpenGL and Vulkan renderers, replaced many container widgets with layout managers, introduced event controllers instead of per-widget event signals and added scalable list views backed by data models. These changes improve performance on large lists and high-resolution displays, but they also explain why porting from GTK 3 takes effort.

Language bindings and tooling

Although the core is C, most GTK code today is written in other languages thanks to GObject Introspection, which generates machine-readable descriptions of the API. Binding layers read those descriptions and expose GTK idiomatically, often automatically for each new release.

The following table shows the common choices and why teams pick them.

LanguageBindingTypical reason to choose it
CNative APISystem components, maximum control
PythonPyGObjectFast prototyping, utilities
Rustgtk4-rsMemory safety with native speed
ValaBuilt-in GObject supportC performance with modern syntax
JavaScriptGJSGNOME Shell extensions and apps
C++gtkmmObject-oriented C++ codebases
Common ways to write GTK applications

Typical projects build with Meson, are developed in GNOME Builder or a general IDE, inspect live interfaces with the built-in GTK Inspector, and are distributed on Linux as Flatpak packages that bundle a known GTK runtime.

GTK compared with Qt and other toolkits

GTK and Qt are the two dominant open-source desktop toolkits on Linux, and the choice between them is usually driven by language, licensing and target desktop. Cross-platform alternatives such as Electron trade native integration for web technologies.

ToolkitCore languageLicense modelMain strength
GTKC with GObjectLGPL onlyNative GNOME integration, many bindings
QtC++LGPL, GPL or commercialBroad platform reach, QML, mobile
wxWidgetsC++Permissive LGPL variantWraps native platform widgets
ElectronJavaScriptMITWeb skills, identical look everywhere
GTK and alternative user interface toolkits

GTK fits best when the target is the Linux desktop, especially GNOME, or when a team prefers C, Rust or Python. On Windows and macOS, GTK applications run well but do not look fully native, and packaging the required libraries adds work compared with platform toolkits.

Common pitfalls when building with GTK

Most GTK problems come from misunderstanding the event model or the version boundaries, not from defects in the toolkit itself. The following table lists frequent symptoms and their remedies.

SymptomLikely causeFix
Random crashes under loadWidgets touched from a worker threadReturn results to the main loop
Frozen windowLong task inside a callbackUse asynchronous GIO or a thread
Code fails to compile on GTK 4GTK 3 tutorial or removed APIFollow the official migration guide
Theme looks brokenHard-coded colors or old CSS selectorsUse style classes and named colors
Memory keeps growingLeaked references or signal handlersDisconnect handlers, check reference counts
Frequent GTK mistakes and fixes

Another frequent trap is mixing documentation versions: many online examples target GTK 3, so developers should always confirm which major version an example uses before copying it.

A non-technical view

GTK is a box of ready-made building blocks for program windows. Instead of drawing every button, menu and text box from scratch, a programmer takes these pieces from GTK and arranges them, much as a builder uses standard doors and windows instead of carving each one by hand.

Many programs on Linux computers, including the familiar GNOME desktop, are built this way. The main benefit is consistency and saved effort: applications look and behave alike, and developers can focus on what their program actually does.

A concrete example

A small engineering team at a research laboratory needs a desktop tool that shows live readings from temperature sensors and lets technicians export the data. They choose GTK 4 with Python through PyGObject, because the lab computers run a GNOME-based Linux distribution and the team already writes its analysis scripts in Python.

The interface is defined in a Blueprint file with a header bar, a list view of sensors and a chart drawn on a custom widget. Sensor polling runs in a background thread, and each new reading is passed to the main loop with an idle callback, so the window never freezes. The application is packaged as a Flatpak.

The result is a responsive tool built in about three weeks that runs identically on every lab workstation and also works on a technician’s Windows laptop with a bundled GTK runtime.