Gtk.MenuButton
Gtk.MenuButton — A widget that shows a popup when clicked on
Object Hierarchy:
GObject
╰── GInitiallyUnowned
╰── Gtk.Widget
╰── Gtk.Container
╰── Gtk.Bin
╰── Gtk.Button
╰── Gtk.ToggleButton
╰── Gtk.MenuButton
Functions:
- new
() -> Gtk.Widget
- set_popup
(self, menu:Gtk.Widget=None)
- get_popup
(self) -> Gtk.Menu
- set_popover
(self, popover:Gtk.Widget=None)
- get_popover
(self) -> Gtk.Popover
- set_menu_model
(self, menu_model:Gio.MenuModel=None)
- get_menu_model
(self) -> Gio.MenuModel
- set_use_popover
(self, use_popover:bool)
- get_use_popover
(self) -> bool
- set_direction
(self, direction:Gtk.ArrowType)
- get_direction
(self) -> Gtk.ArrowType
- set_align_widget
(self, align_widget:Gtk.Widget=None)
- get_align_widget
(self) -> Gtk.Widget
Description:
The Gtk.MenuButton
widget is used to display a popup when clicked on.
This popup can be provided either as a GtkMenu, a Gtk.Popover or an abstract #GMenuModel.
The Gtk.MenuButton
widget can hold any valid child widget. That is, it can hold almost any other standard Gtk.Widget. The most commonly used child is GtkImage. If no widget is explicitely added to the Gtk.MenuButton
, a Gtk.Image is automatically created, using an arrow image oriented according to Gtk.MenuButton
:direction or the generic "view-context-menu" icon if the direction is not set.
The positioning of the popup is determined by the Gtk.MenuButton
:direction property of the menu button.
For menus, the Gtk.Widget:halign and Gtk.Widget:valign properties of the menu are also taken into account. For example, when the direction is %GTK_ARROW_DOWN and the horizontal alignment is %GTK_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
.
Direction = Down
- halign = start ![](down-start.png)
- halign = center ![](down-center.png)
- halign = end
![](down-end.png)
Direction = Up
- halign = start ![](up-start.png)
- halign = center ![](up-center.png)
- halign = end
![](up-end.png)
Direction = Left
- valign = start ![](left-start.png)
- valign = center ![](left-center.png)
- valign = end
![](left-end.png)
Direction = Right
- valign = start ![](right-start.png)
- valign = center ![](right-center.png)
- valign = end ![](right-end.png)
Function Details:
new()
new () -> Gtk.Widget
Creates a new Gtk.MenuButton
widget with downwards-pointingarrow as the only child. You can replace the child widgetwith another Gtk.Widget
should you wish to.
Returns: The newly created
Gtk.MenuButton
widgetSince: 3.6
set_popup()
set_popup (self, menu:Gtk.Widget=None)
Sets the Gtk.Menu
that will be popped up when the button is clicked,or None
to disable the button. If “menu-model” or“popover” are set, they will be set to None
.
- Since: 3.6
get_popup()
get_popup (self) -> Gtk.Menu
Returns the Gtk.Menu
that pops out of the button.If the button does not use a Gtk.Menu
, this functionreturns None
.
Returns: a
Gtk.Menu
orNone
.Since: 3.6
set_popover()
set_popover (self, popover:Gtk.Widget=None)
Sets the Gtk.Popover
that will be popped up when the button isclicked, or None
to disable the button. If “menu-model”or “popup” are set, they will be set to None
.
- Since: 3.12
get_popover()
get_popover (self) -> Gtk.Popover
Returns the Gtk.Popover
that pops out of the button.If the button is not using a Gtk.Popover
, this functionreturns None
.
Returns: a
Gtk.Popover
orNone
.Since: 3.12
set_menu_model()
set_menu_model (self, menu_model:Gio.MenuModel=None)
Sets the GMenuModel from which the popup will be constructed,or None
to disable the button.
Depending on the value of “use-popover”, either aGtk.Menu
will be created with Gtk.Menu:new_from_model`()`, or a`Gtk.Popover` with
Gtk.Popover:new_from_model()
. In either case,actions will be connected as documented for these functions.
If “popup” or “popover” are already set,their content will be lost and replaced by the newly created popup.
- Since: 3.6
get_menu_model()
get_menu_model (self) -> Gio.MenuModel
Returns the GMenuModel used to generate the popup.
Returns: a GMenuModel or
None
.Since: 3.6
set_use_popover()
set_use_popover (self, use_popover:bool)
Sets whether to construct a Gtk.Popover
instead of Gtk.Menuwhen
Gtk.MenuButton:set_menu_model()
is called. Note thatthis property is only consulted when a new menu model is set.
- Since: 3.12
get_use_popover()
get_use_popover (self) -> bool
Returns whether a Gtk.Popover
or a Gtk.Menu
will be constructedfrom the menu model.
Returns:
True
if using a Gtk.PopoverSince: 3.12
set_direction()
set_direction (self, direction:Gtk.ArrowType)
Sets the direction in which the popup will be popped up, aswell as changing the arrow’s direction. The child will notbe changed to an arrow if it was customized.
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 GTK_ARROW_NONE
for a direction
, the popup will behaveas if you passed GTK_ARROW_DOWN
(although you won’t see any arrows).
- Since: 3.6
get_direction()
get_direction (self) -> Gtk.ArrowType
Returns the direction the popup will be pointing at when popped up.
Returns: a
Gtk.ArrowType
valueSince: 3.6
set_align_widget()
set_align_widget (self, align_widget:Gtk.Widget=None)
Sets the Gtk.Widget
to use to line the menu with when popped up.Note that the align_widget
must contain the Gtk.MenuButton
itself.
Setting it to None
means that the menu will be aligned with thebutton itself.
Note that this property is only used with menus currently,and not for popovers.
- Since: 3.6
get_align_widget()
get_align_widget (self) -> Gtk.Widget
Returns the parent Gtk.Widget
to use to line up with menu.
Returns: a
Gtk.Widget
value orNone
.Since: 3.6
Example:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MenuButton(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.connect("destroy", Gtk.main_quit)
menubutton = Gtk.MenuButton("MenuButton")
self.add(menubutton)
menu = Gtk.Menu()
menubutton.set_popup(menu)
for count in range(1, 6):
menuitem = Gtk.MenuItem("Item %i" % (count))
menuitem.connect("activate", self.on_menuitem_activated)
menu.append(menuitem)
menu.show_all()
def on_menuitem_activated(self, menuitem):
print("%s Activated" % (menuitem.get_label()))
window = MenuButton()
window.show_all()
Gtk.main()