Popover

A Popover widget provides a bubble-like context window which is attached to a widget. It is also commonly used for menus in place of a Menu.

Constructor

The Popover is constructed by calling:

popover = Gtk.Popover()

Alternatively, the Popover can be attached to the widget which will launch it by:

popover = Gtk.Popover(relative_to)

When the Popover is to be used as a replacement for a Menu widget, it can be built with a MenuModel listing the menu items:

popover = Gtk.Popover.new_from_model(model)

Methods

Items are added to the Popover with:

popover.add(child)

The child parameter should be set to name of the widget to be added to the Popover. Typically this will be a container such as a Box or Grid from which further widgets can be added.

The Popover is attached to the widget:

popover.set_relative_to(relative_to)

The relative_to parameter should be set to the widget the Popover is to be attached to.

The position of the Popover menu can be set using the method:

popover.set_position(position)

The position value can be set to one of the following:

  • Gtk.PositionType.TOP
  • Gtk.PositionType.BOTTOM
  • Gtk.PositionType.LEFT
  • Gtk.PositionType.RIGHT

In some cases, it may be required to ensure that when the Popover is displayed, it is the focus of the input until closed. This is set with:

popover.set_modal(modal)

When modal is set to True, the Popover will have the keyboard focus.

The default widget within the Popover can be set using the method:

popover.set_default_widget(widget)

Example

Below is an example of a Popover:

#!/usr/bin/env python3

from gi.repository import Gtk


def button_clicked(button):
    popover.show_all()

window = Gtk.Window()
window.set_default_size(250, 250)
window.connect("destroy", Gtk.main_quit)

box = Gtk.Box()
box.set_orientation(Gtk.Orientation.VERTICAL)
window.add(box)

button = Gtk.Button("Popover Launcher")
button.connect("clicked", button_clicked)
box.add(button)

popover = Gtk.Popover()
popover.set_position(Gtk.PositionType.RIGHT)
popover.set_relative_to(button)

box = Gtk.Box()
box.set_spacing(5)
box.set_orientation(Gtk.Orientation.VERTICAL)
popover.add(box)

label = Gtk.Label("A Label widget")
box.add(label)

checkbutton = Gtk.CheckButton("A CheckButton widget")
box.add(checkbutton)

window.show_all()

Gtk.main()

Download: Popover