Skip to content

Gtk.Popover

class — extends Widget, Accessible, Buildable, ConstraintTarget, Native, ShortcutManager

Presents a bubble-like popup.

<picture> <source srcset="popover-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkPopover" src="popover.png"> </picture>

It is primarily meant to provide context-dependent information or options. Popovers are attached to a parent widget. The parent widget must support popover children, as MenuButton and PopoverMenuBar do. If you want to make a custom widget that has an attached popover, you need to call Popover.present in your Widget.size_allocate vfunc, in order to update the positioning of the popover.

The position of a popover relative to the widget it is attached to can also be changed with Popover.set_position. By default, it points to the whole widget area, but it can be made to point to a specific area using Popover.set_pointing_to.

By default, GtkPopover performs a grab, in order to ensure input events get redirected to it while it is shown, and also so the popover is dismissed in the expected situations (clicks outside the popover, or the Escape key being pressed). If no such modal behavior is desired on a popover, Popover.set_autohide may be called on it to tweak its behavior.

GtkPopover as menu replacement

GtkPopover is often used to replace menus. The best way to do this is to use the PopoverMenu subclass which supports being populated from a GMenuModel with PopoverMenu.new_from_model.

<section>
  <attribute name="display-hint">horizontal-buttons</attribute>
  <item>
    <attribute name="label">Cut</attribute>
    <attribute name="action">app.cut</attribute>
    <attribute name="verb-icon">edit-cut-symbolic</attribute>
  </item>
  <item>
    <attribute name="label">Copy</attribute>
    <attribute name="action">app.copy</attribute>
    <attribute name="verb-icon">edit-copy-symbolic</attribute>
  </item>
  <item>
    <attribute name="label">Paste</attribute>
    <attribute name="action">app.paste</attribute>
    <attribute name="verb-icon">edit-paste-symbolic</attribute>
  </item>
</section>

Shortcuts and Gestures

GtkPopover supports the following keyboard shortcuts:

  • <kbd>Escape</kbd> closes the popover.
  • <kbd>Alt</kbd> makes the mnemonics visible.

The following signals have default keybindings:

CSS nodes

popover.background[.menu]
├── arrow
╰── contents
    ╰── <child>

GtkPopover has a main node with name popover, an arrow with name arrow, and another node for the content named contents. The popover node always gets the .background style class. It also gets the .menu style class if the popover is menu-like, e.g. is a PopoverMenu.

Particular uses of GtkPopover, such as touch selection popups or magnifiers in GtkEntry or GtkTextView get style classes like .touch-selection or .magnifier to differentiate from plain popovers.

When styling a popover directly, the popover node should usually not have any background. The visible part of the popover can have a shadow. To specify it in CSS, set the box-shadow of the contents node.

Note that, in order to accomplish appropriate arrow visuals, GtkPopover uses custom drawing for the arrow node. This makes it possible for the arrow to change its shape dynamically, but it also limits the possibilities of styling it using CSS. In particular, the arrow gets drawn over the content node's border and shadow, so they look like one shape, which means that the border width of the content node and the arrow node should be the same. The arrow also does not support any border shape other than solid, no border-radius, only one border width (border-bottom-width is used) and no box-shadow.

Constructors

new

@classmethod
def new(cls) -> Widget

Creates a new GtkPopover.

Methods

get_autohide

def get_autohide(self) -> bool

Returns whether the popover is modal.

See Popover.set_autohide for the implications of this.

get_cascade_popdown

def get_cascade_popdown(self) -> bool

Returns whether the popover will close after a modal child is closed.

get_child

def get_child(self) -> Widget | None

Gets the child widget of popover.

get_has_arrow

def get_has_arrow(self) -> bool

Gets whether this popover is showing an arrow pointing at the widget that it is relative to.

get_mnemonics_visible

def get_mnemonics_visible(self) -> bool

Gets whether mnemonics are visible.

get_offset

def get_offset(self) -> tuple[int, int]

Gets the offset previous set with Popover.set_offset.

get_pointing_to

def get_pointing_to(self) -> tuple[bool, Gdk.Rectangle]

Gets the rectangle that the popover points to.

If a rectangle to point to has been set, this function will return True and fill in rect with such rectangle, otherwise it will return False and fill in rect with the parent widget coordinates.

get_position

def get_position(self) -> PositionType

Returns the preferred position of popover.

popdown

def popdown(self) -> None

Pops popover down.

This may have the side-effect of closing a parent popover as well. See Popover.cascade-popdown.

popup

def popup(self) -> None

Pops popover up.

present

def present(self) -> None

Allocate a size for the GtkPopover.

This function needs to be called in size-allocate by widgets who have a GtkPopover as child. When using a layout manager, this is happening automatically.

To make a popover appear on screen, use Popover.popup.

set_autohide

def set_autohide(self, autohide: bool) -> None

Sets whether popover is modal.

A modal popover will grab the keyboard focus on it when being displayed. Focus will wrap around within the popover. Clicking outside the popover area or pressing Esc will dismiss the popover.

Called this function on an already showing popup with a new autohide value different from the current one, will cause the popup to be hidden.

Parameters:

  • autohideTrue to dismiss the popover on outside clicks

set_cascade_popdown

def set_cascade_popdown(self, cascade_popdown: bool) -> None

If cascade_popdown is True, the popover will be closed when a child modal popover is closed.

If False, popover will stay visible.

Parameters:

  • cascade_popdownTrue if the popover should follow a child closing

set_child

def set_child(self, child: Widget | None = ...) -> None

Sets the child widget of popover.

Parameters:

  • child — the child widget

set_default_widget

def set_default_widget(self, widget: Widget | None = ...) -> None

Sets the default widget of a GtkPopover.

The default widget is the widget that’s activated when the user presses Enter in a dialog (for example). This function sets or unsets the default widget for a GtkPopover.

Parameters:

  • widget — a child widget of popover to set as the default, or None to unset the default widget for the popover

set_has_arrow

def set_has_arrow(self, has_arrow: bool) -> None

Sets whether this popover should draw an arrow pointing at the widget it is relative to.

Parameters:

  • has_arrowTrue to draw an arrow

set_mnemonics_visible

def set_mnemonics_visible(self, mnemonics_visible: bool) -> None

Sets whether mnemonics should be visible.

Parameters:

  • mnemonics_visible — the new value

set_offset

def set_offset(self, x_offset: int, y_offset: int) -> None

Sets the offset to use when calculating the position of the popover.

These values are used when preparing the Gdk.PopupLayout for positioning the popover.

Parameters:

  • x_offset — the x offset to adjust the position by
  • y_offset — the y offset to adjust the position by

set_pointing_to

def set_pointing_to(self, rect: Gdk.Rectangle | None = ...) -> None

Sets the rectangle that popover points to.

This is in the coordinate space of the popover parent.

Parameters:

  • rect — rectangle to point to

set_position

def set_position(self, position: PositionType | int) -> None

Sets the preferred position for popover to appear.

If the popover is currently visible, it will be immediately updated.

This preference will be respected where possible, although on lack of space (eg. if close to the window edges), the GtkPopover may choose to appear on the opposite side.

Parameters:

  • position — preferred popover position

Virtual methods

do_activate_default

def do_activate_default(self) -> None

do_closed

def do_closed(self) -> None

Properties

autohide

autohide: bool  # read/write

Whether to dismiss the popover on outside clicks.

cascade_popdown

cascade_popdown: bool  # read/write

Whether the popover pops down after a child popover.

This is used to implement the expected behavior of submenus.

child

child: Widget  # read/write

The child widget.

default_widget

default_widget: Widget  # read/write

The default widget inside the popover.

has_arrow

has_arrow: bool  # read/write

Whether to draw an arrow.

mnemonics_visible

mnemonics_visible: bool  # read/write

Whether mnemonics are currently visible in this popover.

pointing_to

pointing_to: Gdk.Rectangle  # read/write

Rectangle in the parent widget that the popover points to.

position

position: PositionType | int  # read/write

How to place the popover, relative to its parent.

Signals

activate-default

def on_activate_default(self) -> None: ...

Emitted whend the user activates the default widget.

This is a keybinding signal.

The default binding for this signal is <kbd>Enter</kbd>.

closed

def on_closed(self) -> None: ...

Emitted when the popover is closed.