如果我创建一个应用程序,如下所示:
--------------------------------
image 1|image 2|image 3| (button)
--------------------------------
如果单击按钮,我希望更改应用程序图像:
----------------------------------
image A|image B|image C| (button)
----------------------------------
我如何做到这一点?
这是我的代码:
#include <gtk/gtk.h>
static GtkWidget *image1,*image2,*image3;
static GtkWidget *window;
static GtkWidget *hbox;
static GtkWidget *button;
static void buttonefresh(GtkWidget *button ,gpointer data)
{
image1 = gtk_image_new_from_stock(GTK_STOCK_QUIT,GTK_ICON_SIZE_MENU);
image2 = gtk_image_new_from_stock(GTK_STOCK_QUIT,GTK_ICON_SIZE_MENU);
image3 = gtk_image_new_from_stock(GTK_STOCK_QUIT,GTK_ICON_SIZE_MENU);
gtk_box_pack_start (GTK_BOX (hbox), image1, FALSE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), image2, FALSE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), image3, FALSE, TRUE, 0);
gtk_widget_show (window);
}
int main(int argc, char **argv)
{
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Image Refresh");
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit), NULL);
hbox = gtk_hbox_new (FALSE, 5);
button=gtk_button_new_with_label("Refresh");
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (buttonefresh), NULL);
g_signal_connect (G_OBJECT (button), "destroy",
G_CALLBACK (gtk_main_quit), NULL);
image1 = gtk_image_new_from_stock(GTK_STOCK_OPEN,GTK_ICON_SIZE_MENU);
image2 = gtk_image_new_from_stock(GTK_STOCK_OPEN,GTK_ICON_SIZE_MENU);
image3 = gtk_image_new_from_stock(GTK_STOCK_OPEN,GTK_ICON_SIZE_MENU);
gtk_box_pack_start (GTK_BOX (hbox), image1, FALSE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), image2, FALSE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), image3, FALSE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, TRUE, 0);
gtk_container_add (GTK_CONTAINER (window), hbox);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
答案 0 :(得分:4)
在点击回调中,只需调用任何改变图像的GtkImage方法,例如gtk_image_set_from_image()
。
您可能需要使用gpointer user_data
参数传递足够的数据,以便回调知道要更改哪个GtkImage实例以及将其更改为什么。
你应该不需要重新创建GtkImage小部件,只需更改显示的图像。