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
GtkPopover
that 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_shrink
to 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
label
andicon_name
.If
always_show_arrow
is set toTRUE
anddirection
is 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[[...], None] | None = None, *user_data: Any) → None#
Sets
func
to be called when a popup is about to be shown.func
should use one ofto set a popup for
menu_button
. Iffunc
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 infunc
.- 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 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
NONE
for 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
label
andchild
.If
always_show_arrow
is set toTRUE
anddirection
is 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_name
andchild
.If
direction
is 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
GMenuModel
from which the popup will be constructed.If
menu_model
isNone
, the button is disabled.A
Popover
will be created from the menu model withnew_from_model
. Actions will be connected as documented for this function.If
popover
is already set, it will be dissociated from themenu_button
, and the property is set toNone
.- Parameters:
menu_model – a
GMenuModel
, orNone
to unset and disable the button
- set_popover(popover: Widget | None = None) → None#
Sets the
GtkPopover
that will be popped up when themenu_button
is clicked.If
popover
isNone
, the button is disabled.If
menu_model
is set, the menu model is dissociated from themenu_button
, and the property is set toNone
.- Parameters:
popover – a
GtkPopover
, orNone
to unset and disable the button
Properties#
- class MenuButton