Search code examples
rustgtk3gtk-rs

How do I create a simple Gtk MessageDialog in Rust / gtk-rs?


I am trying out gtk-rs and while there is certainly documentation available, it is much too hard to understand for a beginner. I just see many different impls and traits and generics, but there never are any code examples from which I can learn. Usually I look at code, use it, and then go through it line by line so I can understand it. But this isn't possible here.

Can somebody please help me?

I used to program in PyGTK and I found an old sample code:

def report_error(self, reason):
    dialog = Gtk.MessageDialog(Gtk.Window(), 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "You did something wrong")
    dialog.format_secondary_text(reason)
    dialog.run()
    dialog.destroy()

But how, just how can I do this in Rust/gtk-rs? I am completely lost.


Solution

  • There is an example demonstrating a message box:

    extern crate gtk;
    use gtk::prelude::*;
    use gtk::{ButtonsType, DialogFlags, MessageType, MessageDialog, Window};
    
    fn main() {
        if gtk::init().is_err() {
            println!("Failed to initialize GTK.");
            return;
        }
        MessageDialog::new(None::<&Window>,
                           DialogFlags::empty(),
                           MessageType::Info,
                           ButtonsType::Ok,
                           "Hello World").run();
    }