2017-07-08 289 views
-1

我是使用GTK +製作GUI的新手,我正在使用glade構建GUI,我試圖通過單擊按鈕來更改標籤,這裏是我的代碼:通過點擊按鈕來更改標籤GTK C

#include <stdlib.h> 
#include <gtk/gtk.h> 
#include <gdk/gdkkeysyms.h> 
#include <glib/gprintf.h> 

typedef struct { 
GtkWidget *window1 ; 
GtkWidget *button1; 
GtkWidget *label1; 
}AppWidgets; 

G_MODULE_EXPORT void on_button1_clicked(GtkButton *button, 
        gpointer user_data) 
{ 
    AppWidgets * app = (AppWidgets *) user_data; 
    gtk_label_set_text(GTK_LABEL(app->label1),"Hello World!"); 
} 

int main (int argc, char *argv[]) 
{ 

GtkBuilder *builder; 
AppWidgets *app=g_slice_new(AppWidgets) ; 
GError *err=NULL; 

gtk_init(&argc, &argv); 

builder = gtk_builder_new(); 
gtk_builder_add_from_file (builder, "GUI", &err); 

#define appGET(xx) \ 
app->xx=GTK_WIDGET(gtk_builder_get_object(builder,#xx)) 

appGET(window1); 
appGET(button1); 
appGET(label1); 


gtk_builder_connect_signals(builder, &app); 
g_object_unref(G_OBJECT(builder)); 


gtk_widget_show(app->window1); 
gtk_main(); 
return 0; 
} 

,這裏是我的.glade文件:

<?xml version="1.0"?> 
<!-- Generated with glade 3.18.3 --> 
<interface> 
    <object class="GtkWindow" id="window1"> 
    <property name="can_focus">False</property> 
    <property name="resizable">true</property> 
    <property name="default_width">500</property> 
    <property name="default_height">1000</property> 
    <child> 
     <object class="GtkBox" id="box1"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <property name="orientation">vertical</property> 
     <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="relief">none</property> 
      <property name="focus_on_click">False</property> 
      <signal name="clicked" handler="on_button1_clicked" swapped="no"/> 
      </object> 
      <packing> 
      <property name="expand">True</property> 
      <property name="fill">True</property> 
      <property name="position">0</property> 
      </packing> 
     </child> 
     <child> 
      <object class="GtkLabel" id="label1"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="label" translatable="yes">label</property> 
      </object> 
      <packing> 
      <property name="expand">True</property> 
      <property name="fill">True</property> 
      <property name="position">1</property> 
      </packing> 
     </child> 
     </object> 
    </child> 
    </object> 
</interface> 

這段代碼根本就沒有那麼我應該怎麼做才能改變標籤的文本?

+0

使用指令Im: 克++ -rdynamic -o testGUI -Wall -g main.c中$(pkg配置--cflags --libs GTK + -3.0 gmodule-2.0) –

+0

1)該信息所屬入問題,而不是評論。 2)你正在編譯爲C++。這是與C語言分享一些語法和語法的不同語言。 – Olaf

+0

錯誤究竟是什麼? –

回答

0

編譯代碼,用後:

gcc -o test main.c -rdynamic `pkg-config --cflags --libs gtk+-3.0 gmodule-export-2.0` 

大概gmodule出口-2.0將GTK被列入所以可能是多餘的。

其結果是,該標籤將不會改變,並扔在控制檯的錯誤是:

Gtk-CRITICAL **: gtk_label_set_text: assertion 'GTK_IS_LABEL (label)' failed 

這意味着,APP-> LABEL1不是GtkLabel的。

所以問題是信號的連接:

gtk_builder_connect_signals(builder, &app); 

這裏你應該更改爲:

gtk_builder_connect_signals(builder, app); 

從C API:

無效gtk_builder_connect_signals(GtkBuilder *建設者, gpointer user_data);

+0

謝謝你的工作,我改變了命令爲1你寫和信號連接功能,它的工作 –