When I press the first button and then another, the buttons should swap the images assigned to them. Code is running on:
GCC ..MinGW Windows7(32bit).
gtk+-bundle_2.24.10-win32
I don't get any error while compiling. But I get the following error while running it.
Quote:
(a.exe:2348): Gtk-CRITICAL **: gtk_button_set_image: assertion `image == NULL || GTK_IS_WIDGET (image)' failed
How can I fix this?
Code:
#include<gtk/gtk.h>
#include<stdio.h>
static GtkWidget *prev_b; // previous button click stored here
static GtkWidget *prev_i; // previuos button's image
static int count=0; //distinguish between two button's click
GtkWidget *button1,*button2; //global button widget
GtkWidget *img1; //global image widget1
GtkWidget *img2; // global image widget2
static void on_click(GtkWidget * button, GtkWidget *b);
int main(int argc , char *argv[])
{
GtkWidget * window;
GtkWidget * hbox;
gtk_init(&argc,&argv);
button1=gtk_button_new_with_label("");
button2=gtk_button_new_with_label("");
img1=gtk_image_new_from_file("img/other/h6.png");
img2=gtk_image_new_from_file("img/other/h7.png");
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
hbox=gtk_hbox_new(TRUE,5);
gtk_button_set_image(GTK_BUTTON(button1),img1);
gtk_button_set_image(GTK_BUTTON(button2),img2);
gtk_box_pack_start_defaults(GTK_BOX(hbox),button1);
gtk_box_pack_start_defaults(GTK_BOX(hbox),button2);
gtk_container_add(GTK_CONTAINER(window),hbox);
gtk_widget_show_all(window);
gtk_widget_show(img1);
gtk_widget_show(img2);
g_signal_connect(G_OBJECT(button1),"clicked",G_CALLBACK(on_click),img1);
g_signal_connect(G_OBJECT(button2),"clicked",G_CALLBACK(on_click),img2);
g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
static void on_click(GtkWidget * button, GtkWidget *img)
{
GtkWidget *temp;
temp=gtk_image_new_from_file("img/other/temp.png");
gtk_widget_show(temp);
if(count==1)
{ count=count-1;
if(prev_i==img)return;
else
{
gtk_button_set_image(GTK_BUTTON(prev_b),temp); //setting image to prev button
gtk_button_set_image(GTK_BUTTON(button),prev_i);//setting image to current button
gtk_button_set_image(GTK_BUTTON(prev_b),img);//setting image to current button
}
return ;
}
if(count==0)
{
count=count+1;
prev_b=button;
prev_i=img;
return ;
}
}