Beginner's Second GUI In Rust, Using GTK Tooling

Kent West - kent.west@{that mail that swore to do no evil}

I've been wanting to get my feet wet with GUI programming using Rust, and I finally found a simple enough tool to do so; it's found at Rust GUI Programming.

I'm doing this on a Debian 12 GNU/Linux Box, with Cinnamon as my Desktop. Whereas the Beginner's First GUI Using Rust, on Debian, Using Dioxus Labs Tooling uses a third-party crate from Dioxus Labs, this version using a more standard GTK approach. You may be somewhat aware that much of the GUI work in Linux is based on either the GTK framework or the Qt framework; this is GTK-based.

Install Rust

Install Rust, and make sure you can compile and run a simple "Hello, World" program.

Then create a new project:

Create a new Rust Project
$ cargo new --bin second_gui_gtk

(The --bin is optional; it's assumed if not given.)

Add the GTK Dependency to Crate.toml

Modify 'Crate.toml'
$ cargo add gtk

Create the Program

Then edit the src/main.rs file to the following:

src/main.rs
use gtk::prelude::*;
use gtk::{Window, WindowType};

fn main() {
    gtk::init().expect("Failed to initialize GTK."); // This initializes the GTK (GIMP Tool Kit).
    // If something goes wrong, the program crashes with the failure message.
    
    let window = Window::new(WindowType::Toplevel); // This creates, but does not yet draw, a window.
    window.show_all(); // This does the actual drawing of the window.
    gtk::main(); // This begins the main loop, where the program just sits watching for "events", like a button-click.
                 // Your job now is to figure out how to exit the program.
} // end of main()

Run the Program

And run the program to display a blank window. Simple.

Run the program
$ cargo run

The Result

You'll find that when you close the window, it does not stop the program. For now, just press Ctrl-C to stop the program.

Add a Button

src/main.rs
use gtk::prelude::*;
use gtk::{Button, Window, WindowType};

fn main() {
    gtk::init().expect("Failed to initialize GTK."); // Initializes the GTK (GIMP Tool Kit), with error and crash if it fails.
    let window = Window::new(WindowType::Toplevel); // This creates, but does not yet draw, a window.
    let button = Button::with_label("Click me!"); // This creates, but does not yet draw, a button.
    window.add(&button); // This adds the button to the window.
    window.show_all(); // This does the actual drawing of the window and button.
    gtk::main();// This begins the main loop, where the program just sits watching for "events", like a button-click.
                // Your job now is to figure out how to exit the program, and how to respond to the button-click.
} // end of main()

Again, closing the window does not stop the program, and clicking on this button does not do anything, but you've now started creating a GUI app with GTK from Rust on Debian.

Provide an exit out of the program

src/main.rs
use gtk::prelude::*;
use gtk::{Button, Window, WindowType}; 
use std::process; 

fn main() {
    gtk::init().expect("Failed to initialize GTK."); // Initializes the GTK (GIMP Tool Kit), with error and crash if it fails.
    let window = Window::new(WindowType::Toplevel); // This creates, but does not yet draw, a window.
    let button = Button::with_label("Click me to Exit!"); // This creates, but does not yet draw, a button. 
    button.connect_clicked(move |_| { process::exit(1); });
    window.add(&button); // This adds the button to the window.
    window.show_all(); // This does the actual drawing of the window and button.
    gtk::main();// This begins the main loop, where the program just sits watching for "events", like a button-click.
                // Your job now is to figure out how to exit the program, and how to respond to the button-click.
} // end of main()