DEV Community

Cover image for GTK calculator on Rust
Antonov Mike
Antonov Mike

Posted on

GTK calculator on Rust

(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

Top comments (4)

Collapse
 
ddebajyati profile image
Debajyati Dey

Wow gtk!

Collapse
 
antonov_mike profile image
Antonov Mike

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

Collapse
 
ddebajyati profile image
Debajyati Dey

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.

Thread Thread
 
antonov_mike profile image
Antonov Mike

Well I wish you good luck bro ๐Ÿ˜Ž