Gtk + 3& C&沼气问题 [英] Gtk+3 &C & Glade problems

查看:98
本文介绍了Gtk + 3& C&沼气问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Linux上使用C和Glade编写一个简单的Gui程序. 我编写了一个简单的程序,并使用Glade设计了一个窗口. 当我运行代码时,它说:

I tried to make a simple Gui program with C and Glade on Linux. I wrote a simple program and design a window with Glade. When I run the code it says:

(gtk-test:23026): Gtk-CRITICAL **: gtk_widget_show: assertion ‘GTK_IS_WIDGET(widget)’ failed

并且没有打开任何窗口. 我在互联网上搜索了一下,但没有任何帮助.有人说我必须将林间空档文件转换为.xml,但这没有用.

And no Window open. I searched a bit on the internet but I cant anything helpful. Some say that I have to convert the glade file to .xml but that didn't work.

#include <gtk/gtk.h>

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

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "window_main.glade", NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
    gtk_builder_connect_signals(builder, NULL);

    g_object_unref(builder);

    gtk_widget_show(window);                
    gtk_main();

    return 0;
}

void on_window_main_destroy()
{
    gtk_main_quit();
}

刀刃

<?xml version=1.0 encoding="UFT-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
    <requires lib="gtk+" version="3.12"/>
    <object class="GtkWindow" id="window_main">
        <property name="can_focus">False</property>
        <property name="title" translatable="yes">Test Window</property>
        <property name="default_width">640</property>
        <property name="default_height">480</property>
        <signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
        <child>
            <placeholder/>
        </child>
    </object>
</interface>

推荐答案

似乎您手动添加了xml标记,而且格式不正确.同时,您没有从gtk_builder_add_from_file函数进行任何错误检查.

It seems you add the xml tag by hand and it's malformed. At the same time you are not doing any error checking from the gtk_builder_add_from_file function.

您的xml开头:

<?xml version=1.0 encoding=UFT-8>

,应为:

<?xml version="1.0" encoding="UTF-8"?>

为避免这种情况,您应该使用GError并使用gtk_builder_add_from_file解析构建器文件时检查是否有错误.

To avoid this situation you should use GError and check if there are errors when parsing the builder file with gtk_builder_add_from_file.

我看到您已更新问题中的林间空地文件.如果这样做,答案可能并不明显.无论如何,这是您的代码,其中包含错误检查glade文件是否存在的代码,以及@underscore_d有关检查GtkBuilder get_object函数的提示(可以使用g_assert宏):

I saw that you have updated the glade file in your question. If you do so then the answer may not be obvious. Anyway, here is your code with error checking for glade file existence and @underscore_d tip on checking the GtkBuilder get_object function (could use g_assert macro instead):

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
    GError          *err = NULL;
    GtkBuilder      *builder; 
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "window_main.glade", &err);

    if (err != NULL) {
        fprintf (stderr, "Unable to read file: %s\n", err->message);
        g_error_free(err);
        return 1;
    }

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));

    if (window == NULL || !GTK_IS_WINDOW(window)) {
        fprintf (stderr, "Unable to get window. (window == NULL || window != GtkWindow)\n");
        return 1;
    }

    gtk_builder_connect_signals(builder, NULL);

    g_object_unref(builder);

    gtk_widget_show(window);                
    gtk_main();

    return 0;
}

void on_window_main_destroy(GtkWidget *widget, gpointer user_data)
{
    gtk_main_quit();
}

编译为:

gcc -rdynamic -o window main.c `pkg-config --cflags --libs gtk+-3.0`

这篇关于Gtk + 3&amp; C&amp;沼气问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆