Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用PyGObject中的GtkSourceView从Glade加载GUI_Python_Glade_Pygobject_Gtksourceview - Fatal编程技术网

Python 使用PyGObject中的GtkSourceView从Glade加载GUI

Python 使用PyGObject中的GtkSourceView从Glade加载GUI,python,glade,pygobject,gtksourceview,Python,Glade,Pygobject,Gtksourceview,我试图使用一个在PyGObject中有GtkSourceView小部件的Glade文件。我已经就如何开始在Glade中使用新的GtkSourceView 3.0编写了一个小指南: 问题是当我想从PyGObject加载该Glade时: from gi.repository import Gtk, GtkSource from os.path import abspath, dirname, join WHERE_AM_I = abspath(dirname(__file__)) class M

我试图使用一个在PyGObject中有GtkSourceView小部件的Glade文件。我已经就如何开始在Glade中使用新的GtkSourceView 3.0编写了一个小指南:

问题是当我想从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'
回溯(最近一次呼叫最后一次):
文件“test.py”,第15行,在
gui=MyApp()
文件“test.py”,第11行,在_init中__
self.builder.add_from_文件(self.glade_文件)
文件“/usr/lib/python2.7/dist packages/gi/types.py”,第43行,在函数中
返回信息。调用(*args,**kwargs)
gi._glib.GError:无效的对象类型“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>

假的
真的
真的
真的
2.
2.
4.
真的
假的
我现在不知道如何解决这个问题。我想在从_file()调用add_之前需要注册一些类型,不是吗?任何想法都欢迎

我正在使用:

  • UbuntuPrecision12.04
  • 格拉德3.12.0
  • libgtksourceview 3.0
  • Gtk+3.0

好心的问候

我想起来了:D我只需要在GObject中注册新类型,然后调用add\u from\u file()。只需在imports from gi.repository中添加GObject并调用type_register(),如下所示:

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
我将用此信息更新页面

问候