Using GTK and Rust. Display image on button click but change image based on variable

Viewed 349

I'm trying to make a Blackjack game using rust, the main logic is going fairly well so I started implementing a GUI using GTK but having some issues changing the image dynamically.

When I click hit I want to run my hit function which will pop a card out of the deck and check for bust. I then want the card to be displayed on the screen. I'm using glade to set it all up however I don't know how to change the path of the image file. I can select the path in glade but obviously I need it to update based on the card which has been drawn.

This is the code so far which I copied from a tutorial and modified slightly.

use gio::prelude::*;
use gtk::prelude::*;
use gtk::Application;
use std::env;

pub fn new() {
    let app = Application::new(Some("com.game"), gio::ApplicationFlags::FLAGS_NONE)
        .expect("Failed to initialize GTK");

    app.connect_activate(|app| {
        let glade_src = include_str!("../layout.glade");
        let builder = gtk::Builder::from_string(glade_src);
        let window: gtk::Window = builder.get_object("background").unwrap();
        window.set_application(Some(app));
        let hit_btn: gtk::Button = builder.get_object("hit_btn").unwrap();

        let img_out: gtk::Image = builder.get_object("player_card_one").unwrap();
        let img_out_clone = img_out.clone();

        hit_btn.connect_clicked(move |_| {
            img_out_clone.show();
        });

        window.show_all();
        img_out.hide();
    });

    app.run(&env::args().collect::<Vec<_>>());
}
0 Answers
Related