2

为什么 GTK+ 3 会抛出这个错误?

(.:11977): Gtk-ERROR **: 21:10:01.149: failed to add UI: Errore alla riga 1 carattere 1: Il documento deve iniziare con un elemento (es. <book>)

从意大利语翻译成英语:

(.:11977): Gtk-ERROR **: 21:10:01.149: failed to add UI: Error on line 1 char 1: Document must begin with an element (e.g. <book>)

该错误表明 Rust 正在尝试打开一个空文件(使用 Glade 制作),或者它的语法错误。

我的 Rust 文件编译没有任何错误:

extern crate gtk;

use gtk::traits::*;
use gtk::Builder;
use gtk::Inhibit;

fn main() {
    // Initialise gtk components
    if gtk::init().is_err() {
        println!("Unable to load GTK.");
        return;
    }

    // Load glade file
    let builder = Builder::new_from_string("mainWindow.glade");

    // Create Window
    let window: gtk::Window = builder.get_object("mainWindow").unwrap();

    // Set close event
    window.connect_delete_event(|_, _| {
        gtk::main_quit();
        Inhibit(true)
    });

    // Show the window and call the main() loop of gtk
    window.show_all();
    gtk::main();
}

锈文件

Glade 文件是:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
    <requires lib="gtk+" version="3.20"/>
    <!-- interface-css-provider-path style.css -->
    <object class="GtkWindow" id="mainWindow">
        <property name="can_focus">False</property>
        <property name="window_position">center</property>
        <property name="default_width">500</property>
        <property name="default_height">350</property>
        <child type="titlebar">
            <object class="GtkHeaderBar">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="title" translatable="yes">rustup-gui</property>
                <property name="subtitle" translatable="yes">A simple GUI for rustup.</property>
                <property name="show_close_button">True</property>
                <child>
                    <object class="GtkMenuBar">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <child>
                            <object class="GtkMenuItem">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="tooltip_text" translatable="yes">Here you can find some basic action which can be done with rustup.</property>
                                <property name="label" translatable="yes">Rustup</property>
                                <property name="use_underline">True</property>
                                <child type="submenu">
                                    <object class="GtkMenu">
                                        <property name="visible">True</property>
                                        <property name="can_focus">False</property>
                                        <child>
                                            <object class="GtkMenuItem">
                                                <property name="visible">True</property>
                                                <property name="can_focus">False</property>
                                                <property name="tooltip_text" translatable="yes">Get all the info related to the current rustup installation.</property>
                                                <property name="label" translatable="yes">Info</property>
                                                <property name="use_underline">True</property>
                                            </object>
                                        </child>
                                    </object>
                                </child>
                            </object>
                        </child>
                        <child>
                            <object class="GtkMenuItem">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="label" translatable="yes">Set...</property>
                                <property name="use_underline">True</property>
                                <child type="submenu">
                                    <object class="GtkMenu">
                                        <property name="visible">True</property>
                                        <property name="can_focus">False</property>
                                        <child>
                                            <object class="GtkMenuItem">
                                                <property name="visible">True</property>
                                                <property name="can_focus">False</property>
                                                <property name="label" translatable="yes">Default toolchain</property>
                                                <property name="use_underline">True</property>
                                            </object>
                                        </child>
                                    </object>
                                </child>
                            </object>
                        </child>
                    </object>
                </child>
            </object>
        </child>
        <child>
            <object class="GtkBox">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="orientation">vertical</property>
                <child>
                    <object class="GtkBox">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <child>
                            <object class="GtkCheckButton">
                                <property name="label" translatable="yes">Vervose output (-v)</property>
                                <property name="visible">True</property>
                                <property name="can_focus">True</property>
                                <property name="receives_default">False</property>
                                <property name="draw_indicator">True</property>
                                <signal name="toggled" handler="tglVerbose_Toggled" swapped="no"/>
                            </object>
                            <packing>
                                <property name="expand">False</property>
                                <property name="fill">True</property>
                                <property name="position">0</property>
                            </packing>
                        </child>
                        <child>
                            <object class="GtkLabel">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="margin_left">5</property>
                                <property name="margin_right">5</property>
                                <property name="margin_top">5</property>
                                <property name="margin_bottom">5</property>
                                <property name="label" translatable="yes">Custom command</property>
                            </object>
                            <packing>
                                <property name="expand">False</property>
                                <property name="fill">True</property>
                                <property name="position">1</property>
                            </packing>
                        </child>
                        <child>
                            <object class="GtkEntry">
                                <property name="visible">True</property>
                                <property name="can_focus">True</property>
                                <property name="margin_left">5</property>
                                <property name="margin_right">5</property>
                                <property name="margin_top">5</property>
                                <property name="margin_bottom">5</property>
                                <property name="placeholder_text" translatable="yes">rustup ...</property>
                            </object>
                            <packing>
                                <property name="expand">True</property>
                                <property name="fill">True</property>
                                <property name="position">2</property>
                            </packing>
                        </child>
                    </object>
                    <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">0</property>
                    </packing>
                </child>
                <child>
                    <object class="GtkFrame">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="margin_left">5</property>
                        <property name="margin_right">5</property>
                        <property name="margin_top">5</property>
                        <property name="margin_bottom">5</property>
                        <property name="label_xalign">0.5</property>
                        <property name="shadow_type">in</property>
                        <child>
                            <object class="GtkAlignment">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <child>
                                    <object class="GtkTextView" id="txtOutput">
                                        <property name="width_request">100</property>
                                        <property name="height_request">130</property>
                                        <property name="visible">True</property>
                                        <property name="can_focus">True</property>
                                        <property name="margin_left">5</property>
                                        <property name="margin_right">5</property>
                                        <property name="margin_top">5</property>
                                        <property name="margin_bottom">5</property>
                                        <property name="hexpand">True</property>
                                        <property name="vexpand">True</property>
                                        <property name="editable">False</property>
                                        <property name="monospace">True</property>
                                    </object>
                                </child>
                            </object>
                        </child>
                        <child type="label">
                            <object class="GtkLabel">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="label" translatable="yes">Console output</property>
                            </object>
                        </child>
                    </object>
                    <packing>
                        <property name="expand">True</property>
                        <property name="fill">True</property>
                        <property name="position">1</property>
                    </packing>
                </child>
            </object>
        </child>
    </object>
</interface>

林间档案

我试图搜索这个错误,但似乎除了我之外没有人遇到过这个尴尬的错误。也许它与我的操作系统有关?我正在使用 Pop!_OS,这是一个基于 Ubuntu 的 Linux 发行版,由 System76 制作。

4

2 回答 2

4

该函数gtk::Builder::new_from_string()需要一个包含完整 XML内容的字符串,而不是文件名。您正在传递"mainWindow.glade",这不是有效的 XML 语法,因此您收到一个错误(在第 1 行,因为您只写了一行)。

你想要的是调用:

Builder::new_from_file("mainWindow.glade")

PS:Rust 编译器的一个不错的特性是您可以将文件包含为文字字符串,因此您可以编写:

Builder::new_from_string(include_str!("mainWindow.glade"))

您将把文件的内容编译到程序中。

于 2018-07-20T21:36:32.047 回答
0

当我尝试在 Visual Studio 2017 或文本编辑器上编辑 GUI xml 文件并放置 GUI xml 文件来运行它时,我遇到了同样的问题。

修复方法:

  1. 通过 glade 设计您的 GUI,并根据需要保存(推荐 GtkBuilder 文件格式)。

  2. 替换您在 1 中保存的文件名。

    2.1 c#, new_from_file("你的文件名")

    2.1 c++, gtk_builder_add_from_file(...., "你的文件名", ....)

  3. 你真高兴!

于 2018-09-28T16:02:01.607 回答