2009-10-14 60 views
1

我想在用戶單擊一個buttion時更改GtkImage上的圖像,所以我嘗試了下面的代碼,但它不起作用。如何更改應用程序運行時GtkImage的「文件」屬性

/* 
* Compile me with: 
* gcc -o tut tut.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0) 
*/ 

#include <gtk/gtk.h> 


G_MODULE_EXPORT gboolean on_button1_clicked(
       GtkWidget  *event_box, 
          GdkEventButton *event, 
          gpointer  data) 
{ 
    GtkImage *image; 
    image = GTK_IMAGE(data); 
    g_return_val_if_fail(image, FALSE); 

    g_object_set(image, "file","/home/2.png", NULL); 
    g_print ("Event box clicked at coordinates %f,%f\n", 
      event->x, event->y); 
    /* Returning TRUE means we handled the event, so the signal 
    * emission should be stopped (don't call any further 
    * callbacks that may be connected). Return FALSE 
    * to continue invoking callbacks. 
    */ 
    return TRUE; 
} 
int 
main(int argc, 
     char **argv) 
{ 
    GtkBuilder *builder; 
    GtkWidget *window; 
    GtkWidget *image; 
    GError  *error = NULL; 

    /* Init GTK+ */ 
    gtk_init(&argc, &argv); 


    /* Create new GtkBuilder object */ 
    builder = gtk_builder_new(); 
    /* Load UI from file. If error occurs, report it and quit application. 
    * Replace "tut.<b style="color:black;background-color:#ffff66">glade</b>" with your saved project. */ 
    if(! gtk_builder_add_from_file(builder, "tut_3.xml", &error)) 
    { 
     g_warning("%s", error->message); 
     g_free(error); 
     return(1); 
    } 

    /* Get main window pointer from UI */ 
    GtkImage image = GTK_WIDGET(gtk_builder_get_object(builder, "image1")); 

    /* Connect signals */ 
    gtk_builder_connect_signals(builder, data->image); 

    /* Destroy builder, since we don't need it anymore */ 
    g_object_unref(G_OBJECT(builder)); 

    /* Show window. All other widgets are automatically shown by GtkBuilder */ 
    gtk_widget_show(data->window); 

    /* Start main loop */ 
    gtk_main(); 

    return(0); 
} 

回答

1

要添加到ptomato的答案,gtk_object_set()應該工作,但它是一種迂迴的做事方式。請看here以獲取更正常的API調用,以便用磁盤文件的上下文替換現有GtkImage的內容。

+0

設置''file''屬性與'gtk_image_set_from_file'相同___。 http://git.gnome.org/cgit/gtk+/tree/gtk/gtkimage.c#n340 – ntd 2009-10-27 19:50:56

3

沒有必要使用GtkEventBox趕上被點擊的按鈕 - 只需連接到GtkButton上的「點擊」信號。

以下是我認爲不需要的代碼的一個版本。

/* 
* Compile me with: 
* gcc -o tut tut.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0) 
*/ 

#include <gtk/gtk.h> 


G_MODULE_EXPORT gboolean on_button1_clicked(
         GtkButton *button, 
         gpointer  data) 
{ 
    GtkImage *image; 
    image = GTK_IMAGE(data); 
    g_return_val_if_fail(image, FALSE); 

    g_object_set(image, "file","icon2.png", NULL); 
    g_print ("Button clicked\n"); 
} 
int 
main(int argc, 
     char **argv) 
{ 
    GtkBuilder *builder; 
    GtkWidget *window; 
    GtkWidget *image; 
    GError  *error = NULL; 

    /* Init GTK+ */ 
    gtk_init(&argc, &argv); 


    /* Create new GtkBuilder object */ 
    builder = gtk_builder_new(); 
    /* Load UI from file. If error occurs, report it and quit application. 
    * Replace "tut.<b style="color:black;background-color:#ffff66">glade</b>" with your saved project. */ 
    if(! gtk_builder_add_from_file(builder, "tut_3.xml", &error)) 
    { 
     g_warning("%s", error->message); 
     g_free(error); 
     return(1); 
    } 

    /* Get main window pointer from UI */ 
    window = GTK_WIDGET(gtk_builder_get_object(builder, "window1")); 
    image = GTK_WIDGET(gtk_builder_get_object(builder, "image1")); 

    /* Connect signals */ 
    gtk_builder_connect_signals(builder, image); 

    /* Destroy builder, since we don't need it anymore */ 
    g_object_unref(G_OBJECT(builder)); 

    /* Show window. All other widgets are automatically shown by GtkBuilder */ 
    gtk_widget_show(window); 

    /* Start main loop */ 
    gtk_main(); 

    return(0); 
} 

這裏是相應的格萊德文件:

<?xml version="1.0"?> 
<interface> 
    <requires lib="gtk+" version="2.16"/> 
    <!-- interface-naming-policy project-wide --> 
    <object class="GtkWindow" id="window1"> 
    <child> 
     <object class="GtkButton" id="button1"> 
     <property name="label" translatable="yes">button</property> 
     <property name="visible">True</property> 
     <property name="can_focus">True</property> 
     <property name="receives_default">True</property> 
     <property name="image">image1</property> 
     <signal name="clicked" handler="on_button1_clicked"/> 
     </object> 
    </child> 
    </object> 
    <object class="GtkImage" id="image1"> 
    <property name="visible">True</property> 
    <property name="pixbuf">icon1.png</property> 
    </object> 
</interface> 

要運行它,你需要把兩個文件,icon1.png和icon2.png,在當前目錄。

PS。如果您沒有發佈Glade文件,我無法確定您的代碼在做什麼。此外,你的代碼甚至沒有編譯,所以如果你確切地說過什麼都不起作用,那將會很不錯。

相關問題