pythongtkgtksourceview

GtkSource module not importing in Python


I am trying to build a source code editor using python and gtk, and I get the following error when I run my script:

AttributeError: 'gi.repository.Gtk' object has no attribute 'Source'

Below is my code, I have installed all dependencies (That I have researched) on Homebrew, still I can't import Gtk.sourceview.

# UI
# Imort modules
import os,sys,pygtk
from gi.repository import Gtk, Vte, GLib
from gi.repository import GObject
from gi.repository import GtkSource



class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self)
        # Window title and Icon
        self.set_title("MaeTrics")
        # Vertical Box
        self.box = Gtk.VBox(homogeneous=False, spacing=0)
        self.add(self.box)
        # Scrolled Text Window
        scrolledwindow1 = Gtk.ScrolledWindow()
        scrolledwindow1.set_hexpand(True)
        scrolledwindow1.set_vexpand(True)
        # Source View
        self.source = Gtk.Source.View()
        self.buffer = self.source.get_buffer()
        self.source.set_show_line_numbers(True)
        scrolledwindow1.add(self.source)
        # Pack everything in vertical box
        self.box.pack_start(scrolledwindow1, True, True, 0)
        # Callback functions
        self.connect("delete-event", Gtk.main_quit)
        self.show_all()



window = MainWindow()
Gtk.main()

Solution

  • It should be

    self.source = GtkSource.View()
    

    This works for me on Debian