GUI Applications
Introduction#
Rust has no own framework for GUI development. Yet there are many bindings to existing frameworks. The most advanced library binding is rust-gtk. A ‘semi’ full list of bindings can be found here
Simple Gtk+ Window with text
Add the Gtk dependecy to your Cargo.toml
:
[dependencies]
gtk = { git = "https://github.com/gtk-rs/gtk.git" }
Create a simple window with the following:
extern crate gtk;
use gtk::prelude::*; // Import all the basic things
use gtk::{Window, WindowType, Label};
fn main() {
if gtk::init().is_err() { //Initialize Gtk before doing anything with it
panic!("Can't init GTK");
}
let window = Window::new(WindowType::Toplevel);
//Destroy window on exit
window.connect_delete_event(|_,_| {gtk::main_quit(); Inhibit(false) });
window.set_title("Stackoverflow. example");
window.set_default_size(350, 70);
let label = Label::new(Some("Some text"));
window.add(&label);
window.show_all();
gtk::main();
}
Gtk+ Window with Entry and Label in GtkBox , GtkEntry signal connection
extern crate gtk;
use gtk::prelude::*;
use gtk::{Window, WindowType, Label, Entry, Box as GtkBox, Orientation};
fn main() {
if gtk::init().is_err() {
println!("Failed to initialize GTK.");
return;
}
let window = Window::new(WindowType::Toplevel);
window.connect_delete_event(|_,_| {gtk::main_quit(); Inhibit(false) });
window.set_title("Stackoverflow. example");
window.set_default_size(350, 70);
let label = Label::new(Some("Some text"));
// Create a VBox with 10px spacing
let bx = GtkBox::new(Orientation::Vertical, 10);
let entry = Entry::new();
// Connect "activate" signal to anonymous function
// that takes GtkEntry as an argument and prints it's text
entry.connect_activate(|x| println!("{}",x.get_text().unwrap()));
// Add our label and entry to the box
// Do not expand or fill, zero padding
bx.pack_start(&label, false, false, 0);
bx.pack_start(&entry, false, false, 0);
window.add(&bx);
window.show_all();
gtk::main();
}