-1

I'm trying to make a simple window with gtk4-rs i honestly can't find documentation for how to call the main gtk loop

use gtk::prelude::*;

fn main() {
    // call gtk::init() to initialize gtk.
    if gtk::init().is_err() {
        println!("Failed to initialize GTK.");
        return;
    }
    let win = gtk::Window::new();
    let lab = gtk::Label::new(Some("Type something"));
    let text_area = gtk::Entry::new();
    
    // create a grid to hold the widgets
    let grid = gtk::Grid::new();
    grid.set_column_spacing(10);
    grid.set_row_spacing(10);
    grid.attach(&lab, 0, 0, 1, 1);
    grid.attach(&text_area, 1, 0, 1, 1);
    
    win.set_child(Some(&grid));
    
    win.show();
}
1

1 Answer 1

1

You are missing GTK's Application, which automatically initializes GTK and creates an event loop:

use gtk::prelude::*;

fn main() {
    let application =
        gtk::Application::new(Some("application-id"), Default::default());
    application.connect_activate(build_ui);
    application.run();
}

fn build_ui(app: &gtk::Application) {
    let win = gtk::ApplicationWindow::new(app);
    let lab = gtk::Label::new(Some("Type something"));
    let text_area = gtk::Entry::new();
    
    // create a grid to hold the widgets
    let grid = gtk::Grid::new();
    grid.set_column_spacing(10);
    grid.set_row_spacing(10);
    grid.attach(&lab, 0, 0, 1, 1);
    grid.attach(&text_area, 1, 0, 1, 1);
    
    win.set_child(Some(&grid));
    
    win.show();
}

The gtk-rs book has more information about this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.