Skip to content

Gtk.MenuButton

class — extends Widget, Accessible, Buildable, ConstraintTarget

Displays a popup when clicked.

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

This popup can be provided either as a GtkPopover or as an abstract GMenuModel.

The GtkMenuButton widget can show either an icon (set with the MenuButton.icon-name property) or a label (set with the MenuButton.label property). If neither is explicitly set, a Image is automatically created, using an arrow image oriented according to MenuButton.direction or the generic “open-menu-symbolic” icon if the direction is not set.

The positioning of the popup is determined by the MenuButton.direction property of the menu button.

For menus, the Widget.halign and Widget.valign properties of the menu are also taken into account. For example, when the direction is ArrowType.DOWN and the horizontal alignment is Align.START, the menu will be positioned below the button, with the starting edge (depending on the text direction) of the menu aligned with the starting edge of the button. If there is not enough space below the button, the menu is popped up above the button instead. If the alignment would move part of the menu offscreen, it is “pushed in”.

start center end
down
up
left
right

CSS nodes

menubutton
╰── button.toggle
    ╰── <content>
         ╰── [arrow]

GtkMenuButton has a single CSS node with name menubutton which contains a button node with a .toggle style class.

If the button contains an icon, it will have the .image-button style class, if it contains text, it will have .text-button style class. If an arrow is visible in addition to an icon, text or a custom child, it will also have .arrow-button style class.

Inside the toggle button content, there is an arrow node for the indicator, which will carry one of the .none, .up, .down, .left or .right style classes to indicate the direction that the menu will appear in. The CSS is expected to provide a suitable image for each of these cases using the -gtk-icon-source property.

Optionally, the menubutton node can carry the .circular style class to request a round appearance.

Accessibility

GtkMenuButton uses the AccessibleRole.button role.

Constructors

new

@classmethod
def new(cls) -> Widget

Creates a new GtkMenuButton widget with downwards-pointing arrow as the only child.

You can replace the child widget with another GtkWidget should you wish to.

Methods

get_active

def get_active(self) -> bool

Returns whether the menu button is active.

get_always_show_arrow

def get_always_show_arrow(self) -> bool

Gets whether to show a dropdown arrow even when using an icon or a custom child.

get_can_shrink

def get_can_shrink(self) -> bool

Retrieves whether the button can be smaller than the natural size of its contents.

get_child

def get_child(self) -> Widget | None

Gets the child widget of menu_button.

get_direction

def get_direction(self) -> ArrowType

Returns the direction the popup will be pointing at when popped up.

get_has_frame

def get_has_frame(self) -> bool

Returns whether the button has a frame.

get_icon_name

def get_icon_name(self) -> str | None

Gets the name of the icon shown in the button.

get_label

def get_label(self) -> str | None

Gets the label shown in the button

get_menu_model

def get_menu_model(self) -> Gio.MenuModel | None

Returns the GMenuModel used to generate the popup.

get_popover

def get_popover(self) -> Popover | None

Returns the GtkPopover that pops out of the button.

If the button is not using a GtkPopover, this function returns None.

get_primary

def get_primary(self) -> bool

Returns whether the menu button acts as a primary menu.

get_use_underline

def get_use_underline(self) -> bool

Returns whether an embedded underline in the text indicates a mnemonic.

popdown

def popdown(self) -> None

Dismiss the menu.

popup

def popup(self) -> None

Pop up the menu.

set_active

def set_active(self, active: bool) -> None

Sets whether the menu button is active.

Parameters:

  • active — whether the menu button is active

set_always_show_arrow

def set_always_show_arrow(self, always_show_arrow: bool) -> None

Sets whether to show a dropdown arrow even when using an icon or a custom child.

Parameters:

  • always_show_arrow — whether to show a dropdown arrow even when using an icon or a custom child

set_can_shrink

def set_can_shrink(self, can_shrink: bool) -> None

Sets whether the button size can be smaller than the natural size of its contents.

For text buttons, setting can_shrink to true will ellipsize the label.

For icon buttons, this function has no effect.

Parameters:

  • can_shrink — whether the button can shrink

set_child

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

Sets the child widget of menu_button.

Setting a child resets MenuButton.label and MenuButton.icon-name.

If MenuButton.always-show-arrow is set to TRUE and MenuButton.direction is not GTK_ARROW_NONE, a dropdown arrow will be shown next to the child.

Parameters:

  • child — the child widget

set_create_popup_func

def set_create_popup_func(self, func: MenuButtonCreatePopupFunc | None = ...) -> None

Sets func to be called when a popup is about to be shown.

func should use one of

to set a popup for menu_button. If func is non-None, menu_button will always be sensitive.

Using this function will not reset the menu widget attached to menu_button. Instead, this can be done manually in func.

Parameters:

  • func — function to call when a popup is about to be shown, but none has been provided via other means, or None to reset to default behavior

set_direction

def set_direction(self, direction: ArrowType | int) -> None

Sets the direction in which the popup will be popped up.

If the button is automatically populated with an arrow icon, its direction will be changed to match.

If the does not fit in the available space in the given direction, GTK will its best to keep it inside the screen and fully visible.

If you pass ArrowType.NONE for a direction, the popup will behave as if you passed ArrowType.DOWN (although you won’t see any arrows).

Parameters:

  • direction — a GtkArrowType

set_has_frame

def set_has_frame(self, has_frame: bool) -> None

Sets the style of the button.

Parameters:

  • has_frame — whether the button should have a visible frame

set_icon_name

def set_icon_name(self, icon_name: str) -> None

Sets the name of an icon to show inside the menu button.

Setting icon name resets MenuButton.label and MenuButton.child.

If MenuButton.always-show-arrow is set to TRUE and MenuButton.direction is not GTK_ARROW_NONE, a dropdown arrow will be shown next to the icon.

Parameters:

  • icon_name — the icon name

set_label

def set_label(self, label: str) -> None

Sets the label to show inside the menu button.

Setting a label resets MenuButton.icon-name and MenuButton.child.

If MenuButton.direction is not GTK_ARROW_NONE, a dropdown arrow will be shown next to the label.

Parameters:

  • label — the label

set_menu_model

def set_menu_model(self, menu_model: Gio.MenuModel | None = ...) -> None

Sets the GMenuModel from which the popup will be constructed.

If menu_model is None, the button is disabled.

A Popover will be created from the menu model with PopoverMenu.new_from_model. Actions will be connected as documented for this function.

If MenuButton.popover is already set, it will be dissociated from the menu_button, and the property is set to None.

Parameters:

  • menu_model — a GMenuModel, or None to unset and disable the button

set_popover

def set_popover(self, popover: Popover | None = ...) -> None

Sets the GtkPopover that will be popped up when the menu_button is clicked.

If popover is None, the button is disabled.

If MenuButton.menu-model is set, the menu model is dissociated from the menu_button, and the property is set to None.

Parameters:

  • popover — a GtkPopover, or None to unset and disable the button

set_primary

def set_primary(self, primary: bool) -> None

Sets whether menu button acts as a primary menu.

Primary menus can be opened with the <kbd>F10</kbd> key.

Parameters:

  • primary — whether the menubutton should act as a primary menu

set_use_underline

def set_use_underline(self, use_underline: bool) -> None

If true, an underline in the text indicates a mnemonic.

Parameters:

  • use_underlineTrue if underlines in the text indicate mnemonics

Properties

active

active: bool  # read/write

Whether the menu button is active.

always_show_arrow

always_show_arrow: bool  # read/write

Whether to show a dropdown arrow even when using an icon or a custom child.

can_shrink

can_shrink: bool  # read/write

Whether the size of the button can be made smaller than the natural size of its contents.

child

child: Widget  # read/write

The child widget.

direction

direction: ArrowType | int  # read/write

The GtkArrowType representing the direction in which the menu or popover will be popped out.

has_frame

has_frame: bool  # read/write

Whether the button has a frame.

icon_name

icon_name: str  # read/write

The name of the icon used to automatically populate the button.

label

label: str  # read/write

The label for the button.

menu_model

menu_model: Gio.MenuModel  # read/write

The GMenuModel from which the popup will be created.

See MenuButton.set_menu_model for the interaction with the MenuButton.popover property.

popover

popover: Popover  # read/write

The GtkPopover that will be popped up when the button is clicked.

primary

primary: bool  # read/write

Whether the menu button acts as a primary menu.

Primary menus can be opened using the <kbd>F10</kbd> key

use_underline

use_underline: bool  # read/write

If set an underscore in the text indicates a mnemonic.

Signals

activate

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

Emitted to when the menu button is activated.

The ::activate signal on GtkMenuButton is an action signal and emitting it causes the button to pop up its menu.