pythongtkwebkitgtk3

gtk-builder-error-quark: invalid object type 'WebKitWebView'


I am getting the following error when trying to get a WebKitWebView object via Gtk.builder():

$ python3 test.py
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    builder.add_from_file("ui.glade")
gi.repository.GLib.Error: gtk-builder-error-quark: ui.glade:31:1 Invalid object type 'WebKitWebView' (6)

Here's my Python code

#!/usr/bin/env python

import gi
gi.require_version('WebKit2', '4.0')
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk
from gi.repository import WebKit2 as Webkit

builder = Gtk.Builder()
builder.add_from_file("ui.glade")

window = builder.get_object("window")
window.set_title("Test")

webview = builder.get_object("webview")

if __name__ == "__main__": 
  window.show_all()
  Gtk.main()

And here's my ui.glade file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <requires lib="webkit2gtk" version="2.12"/>
  <object class="GtkWindow" id="window">
    <property name="width_request">800</property>
    <property name="height_request">600</property>
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkEntry">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="WebKitWebView" id="webview">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <child>
              <placeholder/>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

There's a similar question here, but I honestly didn't "get the point" as the answer said.


Solution

  • Problem

    There is a bug that gtk_builder_get_type_from_name doesn't work with WebKitWebView.

    The problem is it is looking for the symbol web_kit_web_view_get_type, when it needs to look for webkit_web_view_get_type. This is unfortunate. I'm not sure what to do about this.

    Workaround: manually register the type before creating the GtkBuilder. Something like g_object_unref (g_object_ref_sink (webkit_web_view_new ())) should work.

    see https://bugs.webkit.org/show_bug.cgi?id=175937

    There are multiple workarounds possible.

    Workaround 1

    One solution could be to create the web view in code

    webview = Webkit.WebView()
    

    In the ui.glade file, replace WebKitWebView with GtkScrolledWindow, perhaps an id like 'scrolled_window' would be more appropriate. The expand property should then be set to `True'.

    After reading the glade file, you can do the following:

    scrolled_window = builder.get_object("scrolled")
    scrolled_window.add_with_viewport(webview)
    
    webview.load_uri("https://www.google.com")
    

    Workaround 2

    Alternatively, you could just call (the call itself is sufficient, you don't have to assign it to a variable), before calling Gtk.Builder():

    Webkit.WebView() 
    

    This is equivalent to the above mentioned workaround from the linked bug report with a call of webkit_web_view_new().

    However, the expand property should still be set to True for WebKitWebView in the ui.glade file.

    Demo

    On Ubuntu, it would look like this in both cases:

    Demo Webkit.WebView