19 releases

Uses new Rust 2021

0.5.5 Jan 2, 2023
0.5.4 Dec 11, 2022
0.5.2 Nov 17, 2022
0.4.8 Jun 4, 2022
0.0.0 Jul 25, 2019

#1 in GUI

Download history 9126/week @ 2022-10-07 4594/week @ 2022-10-14 4359/week @ 2022-10-21 4675/week @ 2022-10-28 5296/week @ 2022-11-04 5355/week @ 2022-11-11 4286/week @ 2022-11-18 4295/week @ 2022-11-25 4052/week @ 2022-12-02 4896/week @ 2022-12-09 4551/week @ 2022-12-16 4487/week @ 2022-12-23 4437/week @ 2022-12-30 5077/week @ 2023-01-06 3642/week @ 2023-01-13 4242/week @ 2023-01-20

18,208 downloads per month
Used in 54 crates (41 directly)

MIT license

186K SLoC

Rust GTK 4 bindings

The project website is here.

Rust bindings of GTK 4, part of gtk4-rs.

This library contains safe Rust bindings for GTK 4, a multi-platform GUI toolkit. It is a part of gtk-rs.

Most of this documentation is generated from the C API. Until all parts of the documentation have been reviewed there will be incongruities with the actual Rust API.

For a gentle introduction to gtk-rs we recommend the online book GUI development with Rust and GTK 4.

See also:

Minimum supported Rust version

Currently, the minimum supported Rust version is 1.63.0.

"Hello, World!" example program

GTK needs to be initialized before use by calling [init][fn@init]. Creating an [Application][struct@Application] will call [init][fn@init] for you.

The gtk4 crate is usually renamed to gtk. You can find an example in the features section for how to do this globally in your Cargo.toml.

use gtk4 as gtk;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow};

fn main() {
    let app = Application::builder()

    app.connect_activate(|app| {
        // We create the main window.
        let window = ApplicationWindow::builder()
            .title("Hello, World!")

        // Show the window.


The main loop

In a typical GTK application you set up the UI, assign signal handlers and run the main event loop.

use gtk4 as gtk;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};

fn main() {
    let application = Application::builder()

    application.connect_activate(|app| {
        let window = ApplicationWindow::builder()
            .title("First GTK Program")

        let button = Button::with_label("Click me!");
        button.connect_clicked(|_| {




GTK is not thread-safe. Accordingly, none of this crate's structs implement [Send] or [Sync].

The thread where [init][fn@init] was called is considered the main thread. OS X has its own notion of the main thread and [init][fn@init] must be called on that thread. After successful initialization, calling any gtk or [gdk][mod@gdk] functions (including [init][fn@init]) from other threads will panic.

Any thread can schedule a closure to be run by the main loop on the main thread via [glib::idle_add][fn@glib::idle_add] or [glib::timeout_add][fn@glib::timeout_add]. While working with GTK you might need the [glib::idle_add_local][fn@glib::idle_add_local] or [glib::timeout_add_local][fn@glib::timeout_add_local] version without the [Send] bound. Those may only be called from the main thread.


The gtk and [gdk][mod@gdk] crates have some run-time safety and contract checks.

  • Any constructor or free function will panic if called before [init][fn@init] or on a non-main thread.

  • Any [&str] or &Path parameter with an interior null (\0) character will cause a panic.

  • Some functions will panic if supplied out-of-range integer parameters. All such cases will be documented individually but they are not yet.

  • A panic in a closure that handles signals or in any other closure passed to a gtk function will abort the process.


Library versions

By default this crate provides only GTK 4.0 APIs. You can access additional functionality by selecting one of the v4_2, v4_4, etc. features.

Cargo.toml example:

package = "gtk4"
version = "0.x.y"
features = ["v4_2"]

Take care when choosing the version to target: some of your users might not have easy access to the latest ones. The higher the version, the fewer users will have it installed.



We recommend using crates from crates.io, as demonstrated here.

If you want to track the bleeding edge, use the git dependency instead:

gtk = { git = "https://github.com/gtk-rs/gtk4-rs.git", package = "gtk4" }

Avoid mixing versioned and git crates like this:

# This will not compile
gdk = {version = "0.1", package = "gdk4"}
gtk = { git = "https://github.com/gtk-rs/gtk4-rs.git", package = "gtk4" }


Feature Description
v4_10 Enable the new APIs part of the to be released GTK 4.10
v4_8 Enable the new APIs part of GTK 4.8
v4_6 Enable the new APIs part of GTK 4.6
v4_4 Enable the new APIs part of GTK 4.4
v4_2 Enable the new APIs part of GTK 4.2
gnome_43 Enable all version feature flags of this crate and its dependencies to match the GNOME 43 SDK
gnome_42 Enable all version feature flags of this crate and its dependencies to match the GNOME 42 SDK
unsafe-assume-initialized Disable checks that gtk is initialized, for use in C ABI libraries
xml_validation Enable xml_validation feature of gtk4-macros

See Also


The Rust bindings of gtk4 are available under the MIT License, please refer to it.


~226K SLoC