开发者

How to refresh an image in gtk?

If I create an application, like this:

--------------------------------
image 1|image 2|image 3| (button)
--------------------------------

I want the application image to change if I click the button:

----------------------------------
image A|image B|image C| (button)
----------------------------------

How do I accomplish this?

This is my code:

#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;
}


In your callback for the click, just call any of GtkImage's methods that change the image, for instance gtk_image_set_from_image().

You might need to pass along enough data using the gpointer user_data argument so the callback knows which GtkImage instance to change, and what to change it to.

You should not need to re-create the GtkImage widgets, just change the image displayed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜