Search code examples
pythonwebviewgtkwebkitgtk3

Python: WebKit.WebView: how to reload on error?


I recently asked this question, which was answered. I'm trying exactly the same, but with WebKit.WebView with GTK and I'm stuck at the same part.

Goal: Load another URL if the first isn't reachable.

import gi, time
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit', '3.0')
from gi.repository import Gtk, WebKit

browser = WebKit.WebView()
browser.load_uri('http://this-domain-does-not-exist.tld')

def load_error(webview, event, url, error):
  webview.load_uri('http://google.com') # not working

browser.connect('load-error', load_error)

win = Gtk.Window()
win.add(browser)
win.show_all()
Gtk.main()

Any idea? Thanks in advance!


Solution

  • For some reason commands that are run within the error callback are ignored. A fix is to add the fallback uri loading after all other events are processed. Like this:

    from gi.repository import Gtk, WebKit, GLib
    
    ....
    
    def load_error(webview, event, url, error):
        GLib.idle_add(webview.load_uri, 'http://google.com')