2012-05-09 10 views
6

Sto provando a utilizzare un file Glade che ha un widget GtkSourceView in PyGObject. Ho scritto una piccola guida su come iniziare a utilizzare il nuovo gtksourceview 3.0 in Glade: http://cjenkins.wordpress.com/2012/05/08/use-gtksourceview-widget-in-glade/Carica GUI da una radura con GtkSourceView in PyGObject

Il problema è quando voglio caricare che Glade da 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 

Quando eseguo che file ottenuto questo errore:

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' 

Il file Glade (test.glade) è solo una finestra con un widget gtksourceview in esso:

<?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> 

Un modo per risolvere questo è fuori dalla mia conoscenza in questo momento. Suppongo di dover registrare qualche tipo prima di chiamare add_from_file(), no? Ogni idea è benvenuta.

sto usando:

  • Ubuntu Precise 12.04
  • Glade 3.12.0
  • libgtksourceview 3.0
  • Gtk + 3,0

Cordiali saluti

risposta

5

ho pensato che out: DI ha solo bisogno di registrare il nuovo tipo i n GObject prima di chiamare add_from_file() come sospettavo. Solo bisogno di aggiungere GObject delle importazioni dalla gi.repository e chiamare type_register() come questo:

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 

io aggiornare la pagina con queste informazioni.

Cordiali saluti