Gtk.MenuShell

g Atk.ImplementorIface Atk.ImplementorIface Gtk.Widget Gtk.Widget Atk.ImplementorIface->Gtk.Widget GObject.GInterface GObject.GInterface GObject.GInterface->Atk.ImplementorIface Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable GObject.InitiallyUnowned GObject.InitiallyUnowned GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Buildable->Gtk.Widget Gtk.Container Gtk.Container Gtk.MenuShell Gtk.MenuShell Gtk.Container->Gtk.MenuShell Gtk.Widget->Gtk.Container

Subclasses:Gtk.Menu, Gtk.MenuBar

Methods

Inherited:Gtk.Container (35), Gtk.Widget (278), GObject.Object (37), Gtk.Buildable (10)
Structs:Gtk.ContainerClass (5), Gtk.WidgetClass (12), GObject.ObjectClass (5)
  activate_item (menu_item, force_deactivate)
  append (child)
  bind_model (model, action_namespace, with_separators)
  cancel ()
  deactivate ()
  deselect ()
  get_parent_shell ()
  get_selected_item ()
  get_take_focus ()
  insert (child, position)
  prepend (child)
  select_first (search_sensitive)
  select_item (menu_item)
  set_take_focus (take_focus)

Virtual Methods

Inherited:Gtk.Container (10), Gtk.Widget (82), GObject.Object (7), Gtk.Buildable (10)
  do_activate_current (force_hide)
  do_cancel ()
  do_deactivate ()
  do_get_popup_delay ()
  do_insert (child, position)
  do_move_current (direction)
  do_move_selected (distance)
  do_select_item (menu_item)
  do_selection_done ()

Properties

Inherited:Gtk.Container (3), Gtk.Widget (39)
Name Type Flags Short Description
take-focus bool r/w/en A boolean that determines whether the menu grabs the keyboard focus

Style Properties

Inherited:Gtk.Widget (17)

Signals

Inherited:Gtk.Container (4), Gtk.Widget (69), GObject.Object (1)
Name Short Description
activate-current An action signal that activates the current menu item within the menu shell.
cancel An action signal which cancels the selection within the menu shell.
cycle-focus A keybinding signal which moves the focus in the given direction.
deactivate This signal is emitted when a menu shell is deactivated.
insert The ::insert signal is emitted when a new Gtk.MenuItem is added to a Gtk.MenuShell.
move-current An keybinding signal which moves the current menu item in the direction specified by direction.
move-selected The ::move-selected signal is emitted to move the selection to another item.
selection-done This signal is emitted when a selection has been completed within a menu shell.

Fields

Inherited:Gtk.Container (4), Gtk.Widget (69), GObject.Object (1)
Name Type Access Description
container Gtk.Container r  
priv Gtk.MenuShellPrivate r  

Class Details

class Gtk.MenuShell(**kwargs)
Bases:Gtk.Container
Abstract:Yes
Structure:Gtk.MenuShellClass

A Gtk.MenuShell is the abstract base class used to derive the Gtk.Menu and Gtk.MenuBar subclasses.

A Gtk.MenuShell is a container of Gtk.MenuItem objects arranged in a list which can be navigated, selected, and activated by the user to perform application functions. A Gtk.MenuItem can have a submenu associated with it, allowing for nested hierarchical menus.

Terminology

A menu item can be “selected”, this means that it is displayed in the prelight state, and if it has a submenu, that submenu will be popped up.

A menu is “active” when it is visible onscreen and the user is selecting from it. A menubar is not active until the user clicks on one of its menuitems. When a menu is active, passing the mouse over a submenu will pop it up.

There is also is a concept of the current menu and a current menu item. The current menu item is the selected menu item that is furthest down in the hierarchy. (Every active menu shell does not necessarily contain a selected menu item, but if it does, then the parent menu shell must also contain a selected menu item.) The current menu is the menu that contains the current menu item. It will always have a GTK grab and receive all key presses.

activate_item(menu_item, force_deactivate)[source]
Parameters:
  • menu_item (Gtk.Widget) – the Gtk.MenuItem to activate
  • force_deactivate (bool) – if True, force the deactivation of the menu shell after the menu item is activated

Activates the menu item within the menu shell.

append(child)[source]
Parameters:child (Gtk.MenuItem) – The Gtk.MenuItem to add

Adds a new Gtk.MenuItem to the end of the menu shell’s item list.

bind_model(model, action_namespace, with_separators)[source]
Parameters:
  • model (Gio.MenuModel or None) – the Gio.MenuModel to bind to or None to remove binding
  • action_namespace (str or None) – the namespace for actions in model
  • with_separators (bool) – True if toplevel items in shell should have separators between them

Establishes a binding between a Gtk.MenuShell and a Gio.MenuModel.

The contents of shell are removed and then refilled with menu items according to model. When model changes, shell is updated. Calling this function twice on shell with different model will cause the first binding to be replaced with a binding to the new model. If model is None then any previous binding is undone and all children are removed.

with_separators determines if toplevel items (eg: sections) have separators inserted between them. This is typically desired for menus but doesn’t make sense for menubars.

If action_namespace is non-None then the effect is as if all actions mentioned in the model have their names prefixed with the namespace, plus a dot. For example, if the action “quit” is mentioned and action_namespace is “app” then the effective action name is “app.quit”.

This function uses Gtk.Actionable to define the action name and target values on the created menu items. If you want to use an action group other than “app” and “win”, or if you want to use a Gtk.MenuShell outside of a Gtk.ApplicationWindow, then you will need to attach your own action group to the widget hierarchy using Gtk.Widget.insert_action_group(). As an example, if you created a group with a “quit” action and inserted it with the name “mygroup” then you would use the action name “mygroup.quit” in your Gio.MenuModel.

For most cases you are probably better off using Gtk.Menu.new_from_model() or Gtk.MenuBar.new_from_model() or just directly passing the Gio.MenuModel to Gtk.Application.set_app_menu() or Gtk.Application.set_menubar().

New in version 3.6.

cancel()[source]

Cancels the selection within the menu shell.

New in version 2.4.

deactivate()[source]

Deactivates the menu shell.

Typically this results in the menu shell being erased from the screen.

deselect()[source]

Deselects the currently selected item from the menu shell, if any.

get_parent_shell()[source]
Returns:the parent Gtk.MenuShell
Return type:Gtk.Widget

Gets the parent menu shell.

The parent menu shell of a submenu is the Gtk.Menu or Gtk.MenuBar from which it was opened up.

New in version 3.0.

get_selected_item()[source]
Returns:the currently selected item
Return type:Gtk.Widget

Gets the currently selected item.

New in version 3.0.

get_take_focus()[source]
Returns:True if the menu shell will take the keyboard focus on popup.
Return type:bool

Returns True if the menu shell will take the keyboard focus on popup.

New in version 2.8.

insert(child, position)[source]
Parameters:
  • child (Gtk.Widget) – The Gtk.MenuItem to add
  • position (int) – The position in the item list where child is added. Positions are numbered from 0 to n-1

Adds a new Gtk.MenuItem to the menu shell’s item list at the position indicated by position.

prepend(child)[source]
Parameters:child (Gtk.Widget) – The Gtk.MenuItem to add

Adds a new Gtk.MenuItem to the beginning of the menu shell’s item list.

select_first(search_sensitive)[source]
Parameters:search_sensitive (bool) – if True, search for the first selectable menu item, otherwise select nothing if the first item isn’t sensitive. This should be False if the menu is being popped up initially.

Select the first visible or selectable child of the menu shell; don’t select tearoff items unless the only item is a tearoff item.

New in version 2.2.

select_item(menu_item)[source]
Parameters:menu_item (Gtk.Widget) – The Gtk.MenuItem to select

Selects the menu item from the menu shell.

set_take_focus(take_focus)[source]
Parameters:take_focus (bool) – True if the menu shell should take the keyboard focus on popup

If take_focus is True (the default) the menu shell will take the keyboard focus so that it will receive all keyboard events which is needed to enable keyboard navigation in menus.

Setting take_focus to False is useful only for special applications like virtual keyboard implementations which should not take keyboard focus.

The take_focus state of a menu or menu bar is automatically propagated to submenus whenever a submenu is popped up, so you don’t have to worry about recursively setting it for your entire menu hierarchy. Only when programmatically picking a submenu and popping it up manually, the take_focus property of the submenu needs to be set explicitly.

Note that setting it to False has side-effects:

If the focus is in some other app, it keeps the focus and keynav in the menu doesn’t work. Consequently, keynav on the menu will only work if the focus is on some toplevel owned by the onscreen keyboard.

To avoid confusing the user, menus with take_focus set to False should not display mnemonics or accelerators, since it cannot be guaranteed that they will work.

See also Gdk.keyboard_grab()

New in version 2.8.

do_activate_current(force_hide) virtual
Parameters:force_hide (bool) –
do_cancel() virtual

Cancels the selection within the menu shell.

New in version 2.4.

do_deactivate() virtual

Deactivates the menu shell.

Typically this results in the menu shell being erased from the screen.

do_get_popup_delay() virtual
Return type:int
do_insert(child, position) virtual
Parameters:
  • child (Gtk.Widget) – The Gtk.MenuItem to add
  • position (int) – The position in the item list where child is added. Positions are numbered from 0 to n-1

Adds a new Gtk.MenuItem to the menu shell’s item list at the position indicated by position.

do_move_current(direction) virtual
Parameters:direction (Gtk.MenuDirectionType) –
do_move_selected(distance) virtual
Parameters:distance (int) –
Return type:bool
do_select_item(menu_item) virtual
Parameters:menu_item (Gtk.Widget) – The Gtk.MenuItem to select

Selects the menu item from the menu shell.

do_selection_done() virtual

Signal Details

Gtk.MenuShell.signals.activate_current(menu_shell, force_hide)
Signal Name:

activate-current

Flags:

RUN_LAST, ACTION

Parameters:
  • menu_shell (Gtk.MenuShell) – The object which received the signal
  • force_hide (bool) – if True, hide the menu after activating the menu item

An action signal that activates the current menu item within the menu shell.

Gtk.MenuShell.signals.cancel(menu_shell)
Signal Name:cancel
Flags:RUN_LAST, ACTION
Parameters:menu_shell (Gtk.MenuShell) – The object which received the signal

An action signal which cancels the selection within the menu shell. Causes the Gtk.MenuShell ::selection-done signal to be emitted.

Gtk.MenuShell.signals.cycle_focus(menu_shell, direction)
Signal Name:

cycle-focus

Flags:

RUN_LAST, ACTION

Parameters:

A keybinding signal which moves the focus in the given direction.

Gtk.MenuShell.signals.deactivate(menu_shell)
Signal Name:deactivate
Flags:RUN_FIRST
Parameters:menu_shell (Gtk.MenuShell) – The object which received the signal

This signal is emitted when a menu shell is deactivated.

Gtk.MenuShell.signals.insert(menu_shell, child, position)
Signal Name:

insert

Flags:

RUN_FIRST

Parameters:
  • menu_shell (Gtk.MenuShell) – The object which received the signal
  • child (Gtk.Widget) – the Gtk.MenuItem that is being inserted
  • position (int) – the position at which the insert occurs

The ::insert signal is emitted when a new Gtk.MenuItem is added to a Gtk.MenuShell. A separate signal is used instead of Gtk.Container ::add because of the need for an additional position parameter.

The inverse of this signal is the Gtk.Container ::removed signal.

New in version 3.2.

Gtk.MenuShell.signals.move_current(menu_shell, direction)
Signal Name:

move-current

Flags:

RUN_LAST, ACTION

Parameters:

An keybinding signal which moves the current menu item in the direction specified by direction.

Gtk.MenuShell.signals.move_selected(menu_shell, distance)
Signal Name:

move-selected

Flags:

RUN_LAST

Parameters:
  • menu_shell (Gtk.MenuShell) – The object which received the signal
  • distance (int) – +1 to move to the next item, -1 to move to the previous
Returns:

True to stop the signal emission, False to continue

Return type:

bool

The ::move-selected signal is emitted to move the selection to another item.

New in version 2.12.

Gtk.MenuShell.signals.selection_done(menu_shell)
Signal Name:selection-done
Flags:RUN_FIRST
Parameters:menu_shell (Gtk.MenuShell) – The object which received the signal

This signal is emitted when a selection has been completed within a menu shell.

Property Details

Gtk.MenuShell.props.take_focus
Name:take-focus
Type:bool
Default Value:True
Flags:READABLE, WRITABLE, EXPLICIT_NOTIFY

A boolean that determines whether the menu and its submenus grab the keyboard focus. See Gtk.MenuShell.set_take_focus() and Gtk.MenuShell.get_take_focus().

New in version 2.8.