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¶
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¶
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¶
Returns whether the menu button is active.
get_always_show_arrow¶
Gets whether to show a dropdown arrow even when using an icon or a custom child.
get_can_shrink¶
Retrieves whether the button can be smaller than the natural size of its contents.
get_child¶
Gets the child widget of menu_button.
get_direction¶
Returns the direction the popup will be pointing at when popped up.
get_has_frame¶
Returns whether the button has a frame.
get_icon_name¶
Gets the name of the icon shown in the button.
get_label¶
Gets the label shown in the button
get_menu_model¶
Returns the GMenuModel used to generate the popup.
get_popover¶
Returns the GtkPopover that pops out of the button.
If the button is not using a GtkPopover, this function
returns None.
get_primary¶
Returns whether the menu button acts as a primary menu.
get_use_underline¶
Returns whether an embedded underline in the text indicates a mnemonic.
popdown¶
Dismiss the menu.
popup¶
Pop up the menu.
set_active¶
Sets whether the menu button is active.
Parameters:
active— whether the menu button is active
set_always_show_arrow¶
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¶
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¶
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¶
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, orNoneto reset to default behavior
set_direction¶
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— aGtkArrowType
set_has_frame¶
Sets the style of the button.
Parameters:
has_frame— whether the button should have a visible frame
set_icon_name¶
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¶
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¶
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— aGMenuModel, orNoneto unset and disable the button
set_popover¶
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— aGtkPopover, orNoneto unset and disable the button
set_primary¶
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¶
If true, an underline in the text indicates a mnemonic.
Parameters:
use_underline—Trueif underlines in the text indicate mnemonics
Properties¶
active¶
Whether the menu button is active.
always_show_arrow¶
Whether to show a dropdown arrow even when using an icon or a custom child.
can_shrink¶
Whether the size of the button can be made smaller than the natural size of its contents.
child¶
The child widget.
direction¶
The GtkArrowType representing the direction in which the
menu or popover will be popped out.
has_frame¶
Whether the button has a frame.
icon_name¶
The name of the icon used to automatically populate the button.
label¶
The label for the button.
menu_model¶
The GMenuModel from which the popup will be created.
See MenuButton.set_menu_model for the interaction
with the MenuButton.popover property.
popover¶
The GtkPopover that will be popped up when the button is clicked.
primary¶
Whether the menu button acts as a primary menu.
Primary menus can be opened using the <kbd>F10</kbd> key
use_underline¶
If set an underscore in the text indicates a mnemonic.
Signals¶
activate¶
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.