|
In the following program , I have created a thread in the main func . when the window is displayed , it should place an image in each 1 sec , at the imagebox . it does it so good but when you click on the start button and the button_click function is run , the thread no longer work and dosent place an image in the imagebox .
[code=]
#include <gtk/gtk.h>
GtkWidget *image[6];
bool flag=true ;
bool setpic()
{
static int j=0 ;
if (j>5)
j=5;
gtk_image_set_from_file(GTK_IMAGE(image[j]),"images1.png");
j++ ;
return flag ;
}
void *thread_function(void *me)
{
g_timeout_add_seconds(1,GSourceFunc(setpic),NULL);
}
void click_button1()
{
flag=false ;
}
void click_button()
{
for (int i=0 ; i<10 ; i++)
g_print("%i",i);
}
int main(int argc , char *argv[])
{
if (!g_thread_supported())
g_thread_init(NULL);
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *vbox ;
GtkWidget *button=gtk_button_new_with_label("start");
GtkWidget *button1=gtk_button_new_with_label("set flag");
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
for (int i=0 ; i<6 ; i++)
image[i]=gtk_image_new();
gtk_widget_set_size_request(window,300,300);
vbox=gtk_vbox_new(TRUE,0);
gtk_container_add(GTK_CONTAINER(window),vbox);
for (int i=0 ; i<6 ; i++)
gtk_box_pack_start_defaults(GTK_BOX(vbox),image[i]);
gtk_box_pack_start_defaults(GTK_BOX(vbox),button);
gtk_box_pack_start_defaults(GTK_BOX(vbox),button1);
///////
///////////
GThread * a_thread ;
a_thread=g_thread_create(thread_function , NULL , FALSE , NULL);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(click_button),NULL);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(click_button1),NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
[/code]
|