【问题标题】:What is a resource path that have to be used with gtk_builder_new_from_resource?必须与 gtk_builder_new_from_resource 一起使用的资源路径是什么?
【发布时间】:2015-05-18 16:46:30
【问题描述】:

我了解函数 gtk_builder_new_from_filegtk_builder_new_from_string 的参数是什么,但我有点想知道什么是资源路径:

GtkBuilder *
gtk_builder_new_from_resource (const gchar *resource_path);

我找不到任何示例(C、python、vala 或其他我不介意的)。

编辑:解决方案

感谢 gniamt 的帮助,这里有一个 ruby​​ 中的基本示例 (https://github.com/ruby-gnome2/ruby-gnome2):

首先是一个简单的ui文件simple_window.ui

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkWindow" id="window">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkLabel" id="label">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">label</property>
        <property name="ellipsize">end</property>
      </object>
    </child>
  </object>
</interface>

然后创建一个 simple_window.gresource.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/simple_window">
    <file>simple_window.ui</file>
  </gresource>
</gresources>

将其打包为:

glib-compile-resources simple_window.gresource.xml

其中创建了一个 simple_window.gresource 二进制文件。

现在是 ruby​​ 脚本:

#!/usr/bin/env ruby

require "gtk3"

path = File.expand_path(File.dirname(__FILE__))
resource = Gio::Resource.load("#{path}/simple_window.gresource")
Gio::Resources.register(resource)
builder = Gtk::Builder.new(:resource => "/simple_window/simple_window.ui")
window = builder.get_object("window")
window.show_all
Gtk.main

【问题讨论】:

  • GLib.Resource 类的文档提供了可能有所帮助的冗长描述。

标签: ruby gtk3 pygobject ruby-gnome2


【解决方案1】:

标签实际上应该是 pygobject 而不是 pygtk,因为后者不包括 GTK+3,无论如何,如果你想要一个使用来自 python 的 Glib.Resource 的示例,你可以看看gtk-demo,我用过保存 CSS 数据的资源。

您通过使用 XML 表示来描述资源中包含的每个单独文件的位置来准备资源: https://gitlab.gnome.org/GNOME/pygobject/-/blob/master/examples/demo/demos/data/demo.gresource.xml

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/css_accordion">
    <file>css_accordion.css</file>
    <file>reset.css</file>
  </gresource>
  <gresource prefix="/css_basics">
    <file>css_basics.css</file>
    <file>reset.css</file>
  </gresource>
  <gresource prefix="/css_multiplebgs">
    <file>css_multiplebgs.css</file>
    <file>brick.png</file>
    <file>brick2.png</file>
    <file>cssview.css</file>
    <file>reset.css</file>
  </gresource>
</gresources>

然后使用 glib-compile-resources 编译资源 https://developer.gnome.org/gio/stable/glib-compile-resources.html

资源可以在应用程序范围内加载: https://gitlab.gnome.org/GNOME/pygobject/-/blob/master/examples/demo/demo.py#L117

base_path = os.path.abspath(os.path.dirname(__file__))
resource_path = os.path.join(base_path, 'demos/data/demo.gresource')
resource = Gio.Resource.load(resource_path)

然后您可以在需要时加载每个单独的资源: https://gitlab.gnome.org/GNOME/pygobject/-/blob/master/examples/demo/demos/Css/css_accordion.py#L48

bytes = Gio.resources_lookup_data("/css_accordion/css_accordion.css", 0)

我希望这有助于理解如何使用资源,您可以像放置 CSS 文件一样放置 Builder 资源。

【讨论】:

  • 你的信息真的很有用,我也找到了这个链接:blogs.gnome.org/alexl/2012/01/26/resources-in-glib。但我仍在努力使用 gtk_builder_new_from_resource。例如,如果我使用演示中的文件(我重新创建了 demo.gresourcefile),我会遇到这种错误: Gtk-ERROR **: failed to add UI: Error at line ... 文档必须以元素(例如 )。
  • 你能发布一个加载资源的代码的小例子吗?请记住,您在 new_from_resource 中使用的字符串是内部资源的相对路径,前缀+文件名
  • 好的,我已经找到了让它工作的方法,它是一个构建器,所以我必须提供一个完整的 ui 文件。我已经编辑了我的主要帖子以展示它是如何工作的。感谢您的帮助。
猜你喜欢
  • 2019-06-02
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2020-09-17
  • 2014-10-02
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多