GTK calculator on Rust
Antonov Mike

Antonov Mike @antonov_mike

About: Rust / Python enthusiast. Seeking my way in development. Love back-end and command line. Guitar and bass player, songwriter, artist

Location:
Tbilisi, Georgia
Joined:
Jul 16, 2022

GTK calculator on Rust

Publish Date: Nov 21 '22
8 5

(Almost done)

Made my first application with user interface and built it on Linux. Have to improve few issues. Also need to know how to build it for Windows and Mac.

Some issues I have to solve to consider the project complete:

  • Listen for keyboard events (the program have to accept keyboard input even if the cursor is not in the text entry field)
  • Scrollable entry screen with saved data of previous operations
  • Set rounding precision (what is the best way to round calculation results?)
  • Negative numbers issue (now it only works if you remove space between negative sign ant number manually)

I rewrote the part of the code in which buttons are created. Now all the numeric buttons are created inside the for loop and placed in the correct places.

// NUM BUTTONS
for iterator in 0..=9 {
    let button = gtk::Button::with_label(&iterator.to_string());
    let mut column = 0;
    let mut raw = 1;

    button.connect_clicked(clone!( @strong entry => move |_| {
        entry.insert_text(&iterator.to_string(), &mut -1);
    }));

    if iterator == 1 || iterator == 4 || iterator == 7 { column = 0 }
    if iterator == 2 || iterator == 5 || iterator == 8 { column = 1 }
    if iterator == 3 || iterator == 6 || iterator == 9 { column = 2 }
    if iterator == 0 { column = 1 }

    if (1..=3).contains(&iterator) { raw = 1 }
    else if (4..=6).contains(&iterator) { raw = 2 }
    else if (7..=9).contains(&iterator) { raw = 3 }
    else if iterator == 0 { raw = 4 }

    grid.attach(&button, column, raw, 1, 1);
}
Enter fullscreen mode Exit fullscreen mode

Comments 5 total

  • Debajyati Dey
    Debajyati DeyMar 9, 2024

    Wow gtk!

    • Antonov Mike
      Antonov MikeMar 9, 2024

      Looks like you are a huge fan of GTK, don't you?

      • Debajyati Dey
        Debajyati DeyMar 9, 2024

        Yeah u can say so. GTK applications power the linux operating systems. I know C at an intermediate level.
        So it could be very beneficial for me if I could develop some practical applications in C.

      • Justin Gedge
        Justin GedgeJan 2, 2025

        I've been on the fence when it comes to GTK vs Qt... starting to lean heavily towards GTK. I like the guarantied OpenSource license... also- Qt seems to have changes hands a number of times over the past decade or two.

Add comment