Search code examples
pythonpython-3.xwebviewgtkwebkit

Python + Gtk + WebKit: scrollbar height not reset after page change


The following code does not reset the vertical scrollbar after a page changed to a smaller height.

It's reproducible:

  1. Execute the script
    1.1 Google.com loads in fullscreen
    1.2 Vertical scrollbar: initial height
  2. Search for something
    2.1 The page will change
    2.2 the vertical scrollbar increases
  3. Click the Google Logo (upper left)
    3.1 The page will change back to Google.com
    3.2 The vertical scrollbar keeps the height of step 2 instead of being reset to step 1

I guess there must be a setting that I'm missing.

Any idea? Thank you!

import gi

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

if __name__ == '__main__':
    URL = 'http://google.com'

    browser = WebKit.WebView()
    browser.load_uri(URL)

    win = Gtk.Window()
    swin = Gtk.ScrolledWindow()
    swin.add_with_viewport(browser)
    swin.set_hexpand(True)
    swin.set_vexpand(True)
    swin.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
    win.add(swin)
    win.fullscreen()
    win.show_all()
    Gtk.main()

Solution

  • Question: WebKit: scrollbar height not reset after page change

    Using

    swin.add_with_viewport(browser)
    

    This will be very wrong for most widgets that support native scrolling, use instead

    swin.add(browser)
    

    From the Documentation: Gtk 3.0 » Classes

    Gtk.ScrolledWindow.add_with_viewport(child)

    Deprecated since version 3.8: Gtk.Container.add() will automatically add a Gtk.Viewport if the child doesn’t implement Gtk.Scrollable.