使用PyGObject中的GtkSourceView从Glade加载GUI [英] Load GUI from a Glade with GtkSourceView in PyGObject

查看:228
本文介绍了使用PyGObject中的GtkSourceView从Glade加载GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在PyGObject中具有GtkSourceView小部件的Glade文件.我已经写了一些有关如何在Glade中开始使用新的GtkSourceView 3.0的指南:

I'm trying to use a Glade file that has a GtkSourceView widget in PyGObject. I've wrote a little guide on how to start using the new GtkSourceView 3.0 in Glade: http://cjenkins.wordpress.com/2012/05/08/use-gtksourceview-widget-in-glade/

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

The problem is when I want to load that Glade from PyGObject:

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

运行该文件时出现此错误:

When I run that file I got this error:

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小部件的窗口:

The Glade file (test.glade) is just a window with a GtkSourceView widget in it:

<?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()之前注册某种类型,不是吗?任何想法都欢迎.

An how to solve this is out of my knowledge right now. I suppose I need to register some type before calling add_from_file(), not? Any idea is welcome.

我正在使用:

  • Ubuntu Precise 12.04
  • Glade 3.12.0
  • libgtksourceview 3.0
  • Gtk + 3.0

亲切的问候

推荐答案

我知道了:D我只是想在调用 add_from_file()之前先在GObject中注册新类型.只需在gi.repository的导入中添加GObject并像这样调用 type_register():

I figured it out :D I just needed to register the new type in GObject before calling add_from_file() as I suspected. Just needed to add GObject in the imports from gi.repository and call type_register() like this:

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

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

I'll update the page with this info.

亲切的问候

这篇关于使用PyGObject中的GtkSourceView从Glade加载GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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