Why is my up icon is not showing?

Hi there! So I have little program in which up arrow icon does not show, I have checked does it load like this

void set_button_icon(GtkWidget *button, const gchar *icon_name) {
    GdkPixbuf *icon = gdk_pixbuf_new_from_file(icon_name, NULL);
    if (icon == NULL) {
        g_printerr("Error loading image: %s\n", icon_name);
        return;
    }
    GtkWidget *image_widget = gtk_image_new_from_pixbuf(icon);
    gtk_button_set_image(GTK_BUTTON(button), image_widget);
}

And Im not getting an error since icon is there, I have checked for typos and could not find any, here is my code


#include <gtk/gtk.h>

GtkWidget *button_l;
GtkWidget *button_r;
GtkWidget *button_up;
gboolean left_arrow_pressed = FALSE;
gboolean right_arrow_pressed = FALSE;
gboolean up_arrow_pressed = FALSE;

void set_button_icon(GtkWidget *button, const gchar *icon_name) {
    GdkPixbuf *icon = gdk_pixbuf_new_from_file(icon_name, NULL);
    if (icon == NULL) {
        g_printerr("Error loading image: %s\n", icon_name);
        return;
    }
    GtkWidget *image_widget = gtk_image_new_from_pixbuf(icon);
    gtk_button_set_image(GTK_BUTTON(button), image_widget);
}

gboolean check_key_state_l(gpointer user_data) {
    if (left_arrow_pressed) {
        g_print("Left arrow key pressed.\n");
    }
    return TRUE; // Continue the timer
}

gboolean check_key_state_r(gpointer user_data) {
    if (right_arrow_pressed) {
        g_print("Right arrow key pressed.\n");
    }
    return TRUE; // Continue the timer
}

gboolean check_key_state_up(gpointer user_data) {
    if (up_arrow_pressed) {
        g_print("Up arrow key pressed.\n");
    }
    return TRUE; // Continue the timer
}

gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data) {
    if (event->keyval == GDK_KEY_Left) {
        if (!left_arrow_pressed) {
            left_arrow_pressed = TRUE;
            g_timeout_add(100, check_key_state_l, NULL);
        }
        set_button_icon(button_l, "pressed_icon_l.png");
        return TRUE; // Event handled
    } else if (event->keyval == GDK_KEY_Right) {
        if (!right_arrow_pressed) {
            right_arrow_pressed = TRUE;
            g_timeout_add(100, check_key_state_r, NULL);
        }
        set_button_icon(button_r, "pressed_icon_r.png");
        return TRUE; // Event handled
    } else if (event->keyval == GDK_KEY_Up) {
        if (!up_arrow_pressed) {
            up_arrow_pressed = TRUE;
            g_timeout_add(100, check_key_state_up, NULL);
        }
        set_button_icon(button_up, "pressed_icon_up.png");
        return TRUE; // Event handled
    }
    return FALSE; // Event not handled
}

gboolean on_key_release(GtkWidget *widget, GdkEventKey *event, gpointer user_data) {
    if (event->keyval == GDK_KEY_Left) {
        left_arrow_pressed = FALSE;
        set_button_icon(button_l, "initial_icon_l.png");
        return TRUE; // Event handled
    } else if (event->keyval == GDK_KEY_Right) {
        right_arrow_pressed = FALSE;
        set_button_icon(button_r, "initial_icon_r.png");
        return TRUE; // Event handled
    } else if (event->keyval == GDK_KEY_Up) {
        up_arrow_pressed = FALSE;
        set_button_icon(button_up, "initial_icon_up.png");
        return TRUE; // Event handled
    }
    return FALSE; // Event not handled
}

int main(int argc, char *argv[]) {
    GtkBuilder *builder;
    GtkWidget *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file(builder, "GladeUI_2.glade", NULL);
    gtk_builder_connect_signals(builder, NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));

    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);


    button_l = GTK_WIDGET(gtk_builder_get_object(builder, "button_l"));
    button_r = GTK_WIDGET(gtk_builder_get_object(builder, "button_r"));
    button_up = GTK_WIDGET(gtk_builder_get_object(builder, "button_up"));

    set_button_icon(button_l, "initial_icon_l.png");
    set_button_icon(button_r, "initial_icon_r.png");
    set_button_icon(button_up, "initial_icon_up.png");

    g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), NULL);
    g_signal_connect(window, "key-release-event", G_CALLBACK(on_key_release), NULL);

    gtk_widget_show(window);
    gtk_main();

    g_object_unref(builder);

    return 0;
}

Here is how my program looks like, as you can see “up_arrow” icon is missing

Program

Hi,

At first look, it seems a call to gtk_widget_set_visible (TRUE) is missing on image_widget.