Skip to content

Instantly share code, notes, and snippets.

@gicrisf
Created November 8, 2020 00:01
Show Gist options
  • Select an option

  • Save gicrisf/a027e1dd302b4800b8ee373a26a49113 to your computer and use it in GitHub Desktop.

Select an option

Save gicrisf/a027e1dd302b4800b8ee373a26a49113 to your computer and use it in GitHub Desktop.
Connect entry change signal to a function with Gtk-rs
// This example is part of the Zwitterio GTK-rs examples series
extern crate gio;
extern crate gtk;
use gio::prelude::*;
use gtk::prelude::*;
use std::env::args;
fn build_ui(application: &gtk::Application) {
// Make window
let window = gtk::ApplicationWindow::new(application);
// Use grid as container and set margins
let grid = gtk::Grid::new();
grid.set_margin_start(10 as i32);
grid.set_margin_end(10 as i32);
grid.set_margin_top(10 as i32);
grid.set_margin_bottom(10 as i32);
// Entry needs a buffer which store the text value
let buffer = gtk::EntryBuffer::new(Some("start value"));
// Let's assign "start value" as initial value of entry, using `with_buffer(&buffer)`
let entry = gtk::Entry::with_buffer(&buffer);
// Attach the entry widget to the first raw and first column of the grid
grid.attach(&entry, 0, 0, 1, 1);
// Connect every change of entry text to this closure
&entry.connect_changed(move|_| {
// Every change is showed on your terminal
println!("{}", &buffer.get_text().as_str());
});
// Final settings for main window
window.add(&grid);
window.set_title("Zwit Example: Entry Update");
window.set_position(gtk::WindowPosition::Center);
window.show_all();
}
fn main() {
let application =
gtk::Application::new(Some("zwitterio.gtk.rs.examples.entry_update"), Default::default())
.expect("Initialization failed...");
application.connect_activate(|app| {
build_ui(app);
});
application.run(&args().collect::<Vec<_>>());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment