Crate gtk[−][src]
Expand description
Rust GTK 3 bindings
This library contains safe Rust bindings for GTK 3, a multi-platform GUI toolkit. It is a part of gtk-rs.
GTK 3.18 is the lowest supported version for the underlying library.
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.
See also
“Hello, World!” example program
GTK needs to be initialized before use by calling init()
. Creating an
Application
will call init()
for you.
use gtk::prelude::*; use gtk::{Application, ApplicationWindow}; fn main() { let app = Application::builder() .application_id("org.example.HelloWorld") .build(); app.connect_activate(|app| { // We create the main window. let win = ApplicationWindow::builder() .application(app) .default_width(320) .default_height(200) .title("Hello, World!") .build(); // Don't forget to make all widgets visible. win.show_all(); }); app.run(); }
The main loop
In a typical GTK application you set up the UI, assign signal handlers and run the main event loop.
use gtk::prelude::*; use gtk::{Application, ApplicationWindow, Button}; fn main() { let application = Application::builder() .application_id("com.example.FirstGtkApp") .build(); application.connect_activate(|app| { let window = ApplicationWindow::builder() .application(app) .title("First GTK Program") .default_width(350) .default_height(70) .build(); let button = Button::with_label("Click me!"); button.connect_clicked(|_| { eprintln!("Clicked!"); }); window.add(&button); window.show_all(); }); application.run(); }
Threads
GTK is not thread-safe. Accordingly, none of this crate’s structs implement
Send
or Sync
.
The thread where init()
was called is considered the main thread. OS X has
its own notion of the main thread and init()
must be called on that thread.
After successful initialization, calling any gtk
or gdk
functions
(including 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()
or glib::timeout_add()
. While
working with GTK you might need the glib::idle_add_local()
or glib::timeout_add_local()
version without the
Send
bound. Those may only be called from the main thread.
Panics
The gtk
and gdk
crates have some run-time safety and contract checks.
-
Any constructor or free function will panic if called before
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.
Features
Library versions
By default this crate provides only GTK 3.18 APIs. You can access additional
functionality by selecting one of the v3_20
, v3_24
, etc. features.
Cargo.toml
example:
[dependencies.gtk]
version = "0.x.y"
features = ["v3_20"]
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.
Re-exports
pub use ffi; | |
pub use atk; | |
pub use cairo; | |
pub use gdk; | |
pub use gdk_pixbuf; | |
pub use gio; | |
pub use glib; | |
pub use pango; |
Modules
functions | |
prelude | Traits and essential types intended for blanket imports. |
subclass | |
xlib |
Structs
Enums
Constants
Statics
Traits
EditableSignals | |
OverlaySignals | |
SpinButtonSignals |