Viewport

A Viewport adds the ability to scroll widgets which do not natively support scrolling (e.g. Grid, Box).

Constructor

The Viewport can be constructed using the following:

viewport = Gtk.Viewport(hadjustment, vadjustment)

The hadjustment and vadjustment parameters are optional and can be specified Adjustment objects.

Methods

Rather than create Adjustment objects manually, these can be retrieved from the Viewport with:

hadjustment = viewport.get_hadjustment()
vadjustment = viewport.get_vadjustment()

If you do use manually created Adjustment objects, these can be attached after construction by calling:

viewport.set_hadjustment(hadjustment)
viewport.set_vadjustment(vadjustment)

The shadow type places a shadow type around the Viewport, and is set via:

viewport.set_shadow_type(shadow)

The shadow should be set to one of the following:

  • Gtk.ShadowType.NONE - no outline.
  • Gtk.ShadowType.IN - outline bevelled inwards.
  • Gtk.ShadowType.OUT - outline bevelled outwards.
  • Gtk.ShadowType.ETCHED_IN - outline sunken.
  • Gtk.ShadowType.ERCHED_OUT - outline raised.

Example

Below is an example of a Viewport:

#!/usr/bin/env python3

from gi.repository import Gtk

class Viewport(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(-1, 200)
        self.connect("destroy", Gtk.main_quit)

        scrolledwindow = Gtk.ScrolledWindow()
        self.add(scrolledwindow)

        hadjustment = Gtk.Adjustment()
        vadjustment = Gtk.Adjustment()

        viewport = Gtk.Viewport(hadjustment, vadjustment)
        scrolledwindow.add(viewport)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        viewport.add(box)

        for i in range(1, 16):
            button = Gtk.Button(label="Button %s" % i)
            box.pack_start(button, True, True, 0)

window = Viewport()
window.show_all()

Gtk.main()

Download: Viewport