Skip to main content
added 657 characters in body
Source Link
frankenapps
  • 8.7k
  • 8
  • 40
  • 86

The Entry does have a text() method which gives you a GString (because Entry implements Editable), that can be converted to a rust str using as_str().

So in order to get the current text from an entry, you can do the following:

let my_input_value = my_input.text().as_str();

For completeness, here is the updated build_ui function:

pub fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Submit")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let input = gtk::Entry::builder()
        .placeholder_text("input")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let gtk_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .build();
    gtk_box.append(&input);
    gtk_box.append(&button);

    button.connect_clicked(move | _button| {
        println!("{}", input.text().as_str()); 
    });

    let window = ApplicationWindow::builder()
        .application(app)
        .title("gtk-app")
        .child(&gtk_box)
        .default_height(720)
        .default_width(360)
        .build();

    window.present();
}

Edit

Because GString implements Display the following works as well:

println!("{}", input.text());

The Entry does have a text() method which gives you a GString (because Entry implements Editable), that can be converted to a rust str using as_str().

So in order to get the current text from an entry, you can do the following:

let my_input_value = my_input.text().as_str();

For completeness, here is the updated build_ui function:

pub fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Submit")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let input = gtk::Entry::builder()
        .placeholder_text("input")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let gtk_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .build();
    gtk_box.append(&input);
    gtk_box.append(&button);

    button.connect_clicked(move | _button| {
        println!("{}", input.text().as_str()); 
    });

    let window = ApplicationWindow::builder()
        .application(app)
        .title("gtk-app")
        .child(&gtk_box)
        .default_height(720)
        .default_width(360)
        .build();

    window.present();
}

The Entry does have a text() method which gives you a GString (because Entry implements Editable), that can be converted to a rust str using as_str().

So in order to get the current text from an entry, you can do the following:

let my_input_value = my_input.text().as_str();

For completeness, here is the updated build_ui function:

pub fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Submit")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let input = gtk::Entry::builder()
        .placeholder_text("input")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let gtk_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .build();
    gtk_box.append(&input);
    gtk_box.append(&button);

    button.connect_clicked(move | _button| {
        println!("{}", input.text().as_str()); 
    });

    let window = ApplicationWindow::builder()
        .application(app)
        .title("gtk-app")
        .child(&gtk_box)
        .default_height(720)
        .default_width(360)
        .build();

    window.present();
}

Edit

Because GString implements Display the following works as well:

println!("{}", input.text());
added 657 characters in body
Source Link
frankenapps
  • 8.7k
  • 8
  • 40
  • 86

The EntryEntry does have a text()text() method which gives you ana GString (because EditableTextEntry implements Editable), that can be converted to a rust strstr using as_str()as_str().

HereSo in order to get the current text from an entry, you can do the following:

let my_input_value = my_input.text().as_str();

For completeness, here is the updated build_ui function:

pub fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Submit")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let input = gtk::Entry::builder()
        .placeholder_text("input")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let gtk_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .build();
    gtk_box.append(&input);
    gtk_box.append(&button);

    button.connect_clicked(move | _button| {
        println!("{}", input.text().as_str()); 
    });

    let window = ApplicationWindow::builder()
        .application(app)
        .title("gtk-app")
        .child(&gtk_box)
        .default_height(720)
        .default_width(360)
        .build();

    window.present();
}

The Entry does have a text() method which gives you an EditableText, that can be converted to a rust str using as_str().

Here is the updated build_ui function:

pub fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Submit")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let input = gtk::Entry::builder()
        .placeholder_text("input")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let gtk_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .build();
    gtk_box.append(&input);
    gtk_box.append(&button);

    button.connect_clicked(move | _button| {
        println!("{}", input.text().as_str()); 
    });

    let window = ApplicationWindow::builder()
        .application(app)
        .title("gtk-app")
        .child(&gtk_box)
        .default_height(720)
        .default_width(360)
        .build();

    window.present();
}

The Entry does have a text() method which gives you a GString (because Entry implements Editable), that can be converted to a rust str using as_str().

So in order to get the current text from an entry, you can do the following:

let my_input_value = my_input.text().as_str();

For completeness, here is the updated build_ui function:

pub fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Submit")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let input = gtk::Entry::builder()
        .placeholder_text("input")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let gtk_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .build();
    gtk_box.append(&input);
    gtk_box.append(&button);

    button.connect_clicked(move | _button| {
        println!("{}", input.text().as_str()); 
    });

    let window = ApplicationWindow::builder()
        .application(app)
        .title("gtk-app")
        .child(&gtk_box)
        .default_height(720)
        .default_width(360)
        .build();

    window.present();
}
Source Link
frankenapps
  • 8.7k
  • 8
  • 40
  • 86

The Entry does have a text() method which gives you an EditableText, that can be converted to a rust str using as_str().

Here is the updated build_ui function:

pub fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Submit")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let input = gtk::Entry::builder()
        .placeholder_text("input")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let gtk_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .build();
    gtk_box.append(&input);
    gtk_box.append(&button);

    button.connect_clicked(move | _button| {
        println!("{}", input.text().as_str()); 
    });

    let window = ApplicationWindow::builder()
        .application(app)
        .title("gtk-app")
        .child(&gtk_box)
        .default_height(720)
        .default_width(360)
        .build();

    window.present();
}