用户界面 – 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;
}

我的UI是一个简单的窗口,有一个没有任何回调函数的按钮.

我在执行时遇到以下错误

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

最佳答案 更改:

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 – 所有小部件的基类

点赞