-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 Answers1

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.

Paulo Lieuthier
  • 426
  • 7
  • 14