MenuButton#
Superclasses: Widget, InitiallyUnowned, Object
Implemented Interfaces: Accessible, Buildable, ConstraintTarget
The GtkMenuButton widget is used to display a popup when clicked.
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
icon_name property) or a label (set with the
label property). If neither is explicitly set,
a Image is automatically created, using an arrow image oriented
according to direction or the generic
“open-menu-symbolic” icon if the direction is not set.
The positioning of the popup is determined by the
direction property of the menu button.
For menus, the halign and valign
properties of the menu are also taken into account. For example, when the
direction is DOWN and the horizontal alignment is 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 BUTTON role.
Constructors#
Methods#
- class MenuButton
-
- get_always_show_arrow() → bool#
Gets whether to show a dropdown arrow even when using an icon or a custom child.
Added in version 4.4.
- get_can_shrink() → bool#
Retrieves whether the button can be smaller than the natural size of its contents.
Added in version 4.12.
- get_popover() → Popover | None#
Returns the
GtkPopoverthat pops out of the button.If the button is not using a
GtkPopover, this function returnsNone.
- set_active(active: bool) → None#
Sets whether the menu button is active.
Added in version 4.10.
- Parameters:
active – whether the menu button is active
- set_always_show_arrow(always_show_arrow: bool) → None#
Sets whether to show a dropdown arrow even when using an icon or a custom child.
Added in version 4.4.
- Parameters:
always_show_arrow – whether to show a dropdown arrow even when using an icon or a custom child
- set_can_shrink(can_shrink: bool) → None#
Sets whether the button size can be smaller than the natural size of its contents.
For text buttons, setting
can_shrinkto true will ellipsize the label.For icon buttons, this function has no effect.
Added in version 4.12.
- Parameters:
can_shrink – whether the button can shrink
- set_child(child: Widget | None = None) → None#
Sets the child widget of
menu_button.Setting a child resets
labelandicon_name.If
always_show_arrowis set toTRUEanddirectionis notGTK_ARROW_NONE, a dropdown arrow will be shown next to the child.Added in version 4.6.
- Parameters:
child – the child widget
- set_create_popup_func(func: Callable[[MenuButton, Any], None] | None = None, user_data: Any = None) → None#
Sets
functo be called when a popup is about to be shown.funcshould use one ofto set a popup for
menu_button. Iffuncis non-None,menu_buttonwill always be sensitive.Using this function will not reset the menu widget attached to
menu_button. Instead, this can be done manually infunc.- Parameters:
func – function to call when a popup is about to be shown, but none has been provided via other means, or
Noneto reset to default behavioruser_data – user data to pass to
func
- set_direction(direction: ArrowType) → 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
NONEfor adirection, the popup will behave as if you passedDOWN(although you won’t see any arrows).- Parameters:
direction – a
GtkArrowType
- set_has_frame(has_frame: bool) → None#
Sets the style of the button.
- Parameters:
has_frame – whether the button should have a visible frame
- set_icon_name(icon_name: str) → None#
Sets the name of an icon to show inside the menu button.
Setting icon name resets
labelandchild.If
always_show_arrowis set toTRUEanddirectionis notGTK_ARROW_NONE, a dropdown arrow will be shown next to the icon.- Parameters:
icon_name – the icon name
- set_label(label: str) → None#
Sets the label to show inside the menu button.
Setting a label resets
icon_nameandchild.If
directionis notGTK_ARROW_NONE, a dropdown arrow will be shown next to the label.- Parameters:
label – the label
- set_menu_model(menu_model: MenuModel | None = None) → None#
Sets the
GMenuModelfrom which the popup will be constructed.If
menu_modelisNone, the button is disabled.A
Popoverwill be created from the menu model withnew_from_model. Actions will be connected as documented for this function.If
popoveris already set, it will be dissociated from themenu_button, and the property is set toNone.- Parameters:
menu_model – a
GMenuModel, orNoneto unset and disable the button
- set_popover(popover: Widget | None = None) → None#
Sets the
GtkPopoverthat will be popped up when themenu_buttonis clicked.If
popoverisNone, the button is disabled.If
menu_modelis set, the menu model is dissociated from themenu_button, and the property is set toNone.- Parameters:
popover – a
GtkPopover, orNoneto unset and disable the button
Properties#
- class MenuButton