19. Popovers

The Gtk.Popover is a separate window used for displaying additional information and is often used with button menus and context menus. Popovers are visually connected to a related widget with a small triangle. Their uses are similar to those of dialog windows with the advantage of being less disruptive and having a connection with the widget the popover is pointing to.

A Popover can be created with Gtk.Popover; for opening the popover use Gtk.Popover.popup().

19.1. Custom Popover

A widget can be added to a popover using Gtk.Container.add().

19.1.1. Example

_images/popover_example.png
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class PopoverWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Popover Demo")
        self.set_border_width(10)
        self.set_default_size(300, 200)

        outerbox = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL)
        self.add(outerbox)

        self.popover = Gtk.Popover()
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        vbox.pack_start(Gtk.ModelButton(label="Item 1"), False, True, 10)
        vbox.pack_start(Gtk.Label(label="Item 2"), False, True, 10)
        vbox.show_all()
        self.popover.add(vbox)
        self.popover.set_position(Gtk.PositionType.BOTTOM)

        button = Gtk.MenuButton(label="Click Me", popover=self.popover)
        outerbox.pack_start(button, False, True, 0)


win = PopoverWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()