I’m trying to implement a GtkSourceView in Gnome-Builder ( using Python as the language ) and I’m totally unable to get it working.
I have written a small program that works perfectly as a proof of concept, but I’m unable to get it working with Builder.
import gi
gi.require_version("Gtk", "4.0")
gi.require_version("GtkSource", "5")
from gi.repository import GObject, Gtk, GtkSource
def on_activate(app):
# Define Window Properties
win = Gtk.ApplicationWindow(application=app)
win.set_title("test")
win.set_default_size( 800, 600 )
# Create scrollable Container
scroll = Gtk.ScrolledWindow()
# Create and configure codeview with it's settings
codeview = GtkSource.View() # See https://gnome.pages.gitlab.gnome.org/gtksourceview/gtksourceview5/class.View.html
codeview.set_wrap_mode( Gtk.WrapMode.WORD_CHAR )# See https://docs.gtk.org/gtk4/enum.WrapMode.html
codeview.set_monospace( True ) # See https://docs.gtk.org/gtk4/method.TextView.set_monospace.html
codeview.set_smart_home_end( 1 ) # See https://gnome.pages.gitlab.gnome.org/gtksourceview/gtksourceview5/enum.SmartHomeEndType.html
codeview.set_tab_width( 4 ) # See https://gnome.pages.gitlab.gnome.org/gtksourceview/gtksourceview5/method.View.set_tab_width.html
codeview.set_show_line_marks( True ) # See https://gnome.pages.gitlab.gnome.org/gtksourceview/gtksourceview5/method.View.set_show_line_marks.html
codeview.set_show_line_numbers( True ) # See https://gnome.pages.gitlab.gnome.org/gtksourceview/gtksourceview5/method.View.set_show_line_numbers.html
# Prepare a colorscheme
manager = GtkSource.StyleSchemeManager().get_default()
scheme = manager.get_scheme("oblivion")
# Prepare the textbuffer
textbuffer = GtkSource.Buffer()
textbuffer.set_text("teststring", -1)
textbuffer.set_style_scheme(scheme)
# Link all widgets
codeview.set_buffer(textbuffer)
scroll.set_child( codeview )
# Add widgets to window and show it.
win.set_child( scroll )
win.present()
# Define app
app = Gtk.Application(application_id='org.gtk.Example')
# Connect methods
app.connect('activate', on_activate)
# Run the app
app.run(None)
This works, but whenever I try to use GtkSourceView in the UI template in Builder it complains about not knowing the type:
Invalid object type ‘GtkSourceView’
Can somebody please give me a hint where to start and how to implement this? The documentation is very very sparse and I’m having a hard time finding documentation about the Gtk4 Libraries with examples in Python.
Best regards