gtk glade需要帮助

时间:2014-05-20 17:04:58

标签: user-interface gtk glade

我使用glade制作用户界面。

我已成功生成了林地文件

现在我必须在我的C代码中包含此文件。

我正在使用以下代码:

#include <stdlib.h>
#include<gtk/gtk.h>
int main (int argc, char *argv[])
{
GtkWidget *builder,*window,*button;
gtk_init (&argc, &argv);
builder=gtk_builder_new();
gtk_builder_add_from_file(builder,"shiv.glade",NULL);
window=GTK_WIDGET (gtk_builder_get_object(builder,"window1")) ;
button=GTK_WIDGET (gtk_builder_get_object(builder,"button1"));
g_object_unref(G_OBJECT(builder));
gtk_widget_show(button);
gtk_widget_show(window);
gtk_main ();
return 0;
}

我的用户界面是一个简单的窗口,有一个没有任何回调函数的按钮。

我在执行时遇到以下错误

GTK-CRITICAL **: IA__gtk_widget_show assertion 'GTK_IS_WIDGET(widget)' failed

1 个答案:

答案 0 :(得分:2)

变化:

GtkWidget *builder,*window,*button;

使用:

GtkWidget *window,*button;
GtkBuilder *builder;

这应该解决。

实施例

#include <stdlib.h>
#include <gtk/gtk.h>

static void 
close_window ( GtkWidget *widget, gpointer window)
{
    printf("application close...\n");
    gtk_widget_destroy((GtkWidget*)window);
}

int main (int argc, char *argv[])
{
    GtkWidget *window, *button;
    GtkBuilder *builder; 

    gtk_init (&argc, &argv);
    builder=gtk_builder_new();
    gtk_builder_add_from_file(builder,"a.glade",NULL);

    window = GTK_WIDGET (gtk_builder_get_object(builder,"window1")) ;
    button = GTK_WIDGET (gtk_builder_get_object(builder,"button1"));

    g_signal_connect (G_OBJECT (button), "clicked",G_CALLBACK (close_window),window);
    g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window));

    g_object_unref(G_OBJECT(builder));
    gtk_widget_show_all( window );
    gtk_main ();
    return 0;
}

来自GTK3 reference manual
GtkBuilder - 从XML UI定义构建接口;
GtkWidget - 所有小部件的基类