Advertisement
Guest User

Untitled

a guest
Apr 4th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. use gtk::prelude::*;
  2. use gtk::{Application, ApplicationWindow, Box, Button, Label};
  3.  
  4. fn main() {
  5. let app = Application::new(Some("com.example.gtk-app"), Default::default()) ;
  6.  
  7. app.connect_activate(|app| {
  8. let window = ApplicationWindow::new(app);
  9. window.set_title("Counter App");
  10. window.set_default_size(400, 300);
  11.  
  12. let vbox = Box::new(gtk::Orientation::Vertical, 10);
  13. window.add(&vbox);
  14.  
  15. let label = Label::new(Some("0"));
  16. vbox.add(&label);
  17.  
  18. let hbox = Box::new(gtk::Orientation::Horizontal, 10);
  19. vbox.add(&hbox);
  20.  
  21. let decrement_button = Button::with_label("-");
  22. let label_clone = label.clone();
  23. decrement_button.connect_clicked(move |_| {
  24. let value: i32 = label_clone.text().parse().unwrap_or(0);
  25. label_clone.set_text(&(value - 1).to_string());
  26. });
  27. hbox.add(&decrement_button);
  28.  
  29. let increment_button = Button::with_label("+");
  30. let label_clone = label.clone();
  31. increment_button.connect_clicked(move |_| {
  32. let value: i32 = label_clone.text().parse().unwrap_or(0);
  33. label_clone.set_text(&(value + 1).to_string());
  34. });
  35. hbox.add(&increment_button);
  36.  
  37. window.show_all();
  38. });
  39.  
  40. app.run();
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement