使用 PyGObject 中的 GtkSourceView 从 Glade 加载 GUI

2023-12-30

我正在尝试使用 PyGObject 中具有 GtkSourceView 小部件的 Glade 文件。我写了一个关于如何开始在 Glade 中使用新的 GtkSourceView 3.0 的小指南:http://cjenkins.wordpress.com/2012/05/08/use-gtksourceview-widget-in-glade/ http://cjenkins.wordpress.com/2012/05/08/use-gtksourceview-widget-in-glade/

问题是当我想从 PyGObject 加载 Glade 时:

from gi.repository import Gtk, GtkSource
from os.path import abspath, dirname, join

WHERE_AM_I = abspath(dirname(__file__))

class MyApp(object):

    def __init__(self):
        self.builder = Gtk.Builder()
        self.glade_file = join(WHERE_AM_I, 'test.glade')
        self.builder.add_from_file(self.glade_file)

if __name__ == '__main__':
    try:
        gui = MyApp()
        Gtk.main()
    except KeyboardInterrupt:
        pass

当我运行该文件时,我收到此错误:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    gui = MyApp()
  File "test.py", line 11, in __init__
    self.builder.add_from_file(self.glade_file)
  File "/usr/lib/python2.7/dist-packages/gi/types.py", line 43, in function
    return info.invoke(*args, **kwargs)
gi._glib.GError: Invalid object type `GtkSourceView'

Glade 文件 (test.glade) 只是一个带有 GtkSourceView 小部件的窗口:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtksourceview 3.0 -->
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkSourceView" id="gtksourceview1">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="has_tooltip">True</property>
        <property name="left_margin">2</property>
        <property name="right_margin">2</property>
        <property name="tab_width">4</property>
        <property name="auto_indent">True</property>
        <property name="indent_on_tab">False</property>
      </object>
    </child>
  </object>
</interface>

目前我不知道如何解决这个问题。我想我需要在调用 add_from_file() 之前注册某种类型,不是吗?欢迎任何想法。

我在用着:

  • Ubuntu 精确 12.04
  • 林间空地3.12.0
  • libgtksourceview 3.0
  • Gtk+ 3.0

亲切的问候


我想通了:D 我只需要在调用之前在 GObject 中注册新类型从文件中添加()正如我所怀疑的。只需要在 gi.repository 的导入中添加 GObject 并调用类型_寄存器()像这样:

from gi.repository import Gtk, GtkSource, GObject
from os.path import abspath, dirname, join

WHERE_AM_I = abspath(dirname(__file__))

class MyApp(object):

    def __init__(self):
        self.builder = Gtk.Builder()
        self.glade_file = join(WHERE_AM_I, 'test.glade')
        GObject.type_register(GtkSource.View)
        self.builder.add_from_file(self.glade_file)

if __name__ == '__main__':
    try:
        gui = MyApp()
        Gtk.main()
    except KeyboardInterrupt:
        pass

我将使用此信息更新页面。

亲切的问候

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 PyGObject 中的 GtkSourceView 从 Glade 加载 GUI 的相关文章

随机推荐