Gtk.Menu


Gtk.Menu — A menu widget

Object Hierarchy:

    GObject
    ╰── GInitiallyUnowned
        ╰── Gtk.Widget
            ╰── Gtk.Container
                ╰── Gtk.MenuShell
                    ╰── Gtk.Menu
                        ╰── Gtk.RecentChooserMenu

Functions:


Signals:

  • “move-scroll” (menu, scroll_type, user_data)

Description:

A Gtk.Menu is a Gtk.MenuShell that implements a drop down menu consisting of a list of Gtk.MenuItem objects which can be navigated and activated by the user to perform application functions.

A Gtk.Menu is most commonly dropped down by activating a Gtk.MenuItem in a GtkMenuBar or popped up by activating a Gtk.MenuItem in another GtkMenu.

A GtkMenu can also be popped up by activating a Gtk.ComboBox.

Other composite widgets such as the Gtk.Notebook can pop up a Gtk.Menu as well.

Applications can display a Gtk.Menu as a popup menu by calling the Gtk.Menu::popup() function. The example below shows how an application can pop up a menu when the 3rd mouse button is pressed.

Connecting the popup signal handler.
        test_button.connect_object('button-press-event', self.on_pop_menu, menu)

    def on_pop_menu(self, widget, event):
        widget.popup(None, None, None, None, event.button, event.time)
Signal handler which displays a popup menu.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_title("Window")
        self.set_size_request(300, 300)
        self.connect("destroy", Gtk.main_quit)

        grid = Gtk.Grid()
        self.add(grid)

        menu = Gtk.Menu()
        menuitem = Gtk.MenuItem("Test1")
        menu.append(menuitem)
        menuitem.show()
        menuitem = Gtk.MenuItem("Test2")
        menuitem.show()
        menu.append(menuitem)        

        button = Gtk.Button("Test Popup")
        grid.add(button)

        button.connect_object('button-press-event', self.on_pop_menu, menu)

    def on_pop_menu(self, widget, event):
        widget.popup(None, None, None, None, event.button, event.time)

window = Window()
window.show_all()
Gtk.main()

Function Details:

new()

new () -> Gtk.Widget

Creates a new Gtk.Menu

  • Returns: a new Gtk.Menu

new_from_model()

new_from_model (model:Gio.MenuModel) -> Gtk.Widget

Creates a Gtk.Menu and populates it with menu items andsubmenus according to model. The created menu items are connected to actions found in theGtk.ApplicationWindow to which the menu belongs - typicallyby means of being attached to a widget (see Gtk.Menu:attach_to_widget())that is contained within the Gtk.ApplicationWindows widget hierarchy. Actions can also be added using `Gtk.Widget:insert_action_group()` on the menu'sattach widget or on any of its parent widgets.

  • Returns: a new Gtk.Menu

  • Since: 3.4


set_screen()

set_screen (self, screen:Gdk.Screen=None)

Sets the GdkScreen on which the menu will be displayed.

  • Since: 2.2

reorder_child()

reorder_child (self, child:Gtk.Widget, position:int)

Moves child to a new position in the list of menuchildren.


attach()

attach (self, child:Gtk.Widget, left_attach:int, right_attach:int, top_attach:int, bottom_attach:int)

Adds a new Gtk.MenuItem to a (table) menu. The number of “cells” thatan item will occupy is specified by left_attach, right_attach,top_attach and bottom_attach. These each represent the leftmost,rightmost, uppermost and lower column and row numbers of the table.(Columns and rows are indexed from zero). Note that this function is not related to Gtk.Menu:detach().

  • Since: 2.4

popup_for_device()

popup_for_device (self, device:Gdk.Device=None, parent_menu_shell:Gtk.Widget=None, parent_menu_item:Gtk.Widget=None, func:Gtk.MenuPositionFunc=None, data=None, button:int, activate_time:int)

Displays a menu and makes it available for selection. Applications can use this function to display context-sensitive menus,and will typically supply None for the parent_menu_shell,parent_menu_item, func, data and destroy parameters. The defaultmenu positioning function will position the menu at the current positionof device (or its corresponding pointer). The button parameter should be the mouse button pressed to initiatethe menu popup. If the menu popup was initiated by something other thana mouse button press, such as a mouse button release or a keypress,button should be 0. The activate_time parameter is used to conflict-resolve initiation ofconcurrent requests for mouse/keyboard grab requests. To functionproperly, this needs to be the time stamp of the user event (such asa mouse click or key press) that caused the initiation of the popup.Only if no such event is available, `Gtk.get_current_event_time()` canbe used instead.

  • Since: 3.0

popup (self, parent_menu_shell:Gtk.Widget=None, parent_menu_item:Gtk.Widget=None, func:Gtk.MenuPositionFunc=None, data=None, button:int, activate_time:int)

Displays a menu and makes it available for selection. Applications can use this function to display context-sensitivemenus, and will typically supply None for the parent_menu_shell,parent_menu_item, func and data parameters. The default menupositioning function will position the menu at the current mousecursor position. The button parameter should be the mouse button pressed to initiatethe menu popup. If the menu popup was initiated by something otherthan a mouse button press, such as a mouse button release or a keypress,button should be 0. The activate_time parameter is used to conflict-resolve initiationof concurrent requests for mouse/keyboard grab requests. To functionproperly, this needs to be the timestamp of the user event (such asa mouse click or key press) that caused the initiation of the popup.Only if no such event is available, `Gtk.get_current_event_time()` canbe used instead.


set_accel_group()

set_accel_group (self, accel_group:Gtk.AccelGroup=None)

Set the Gtk.AccelGroup which holds global accelerators for themenu. This accelerator group needs to also be added to all windowsthat this menu is being used in with `Gtk.Window:add_accel_group()`,in order for those windows to support all the acceleratorscontained in this group.


get_accel_group()

get_accel_group (self) -> Gtk.AccelGroup

Gets the Gtk.AccelGroup which holds global accelerators for themenu. See Gtk.Menu:set_accel_group().

  • Returns: the Gtk.AccelGroup associated with the menu.

set_accel_path()

set_accel_path (self, accel_path:str=None)

Sets an accelerator path for this menu from which accelerator pathsfor its immediate children, its menu items, can be constructed.The main purpose of this function is to spare the programmer theinconvenience of having to call Gtk.Menu:item_set_accel_path() oneach menu item that should support runtime user changable accelerators.Instead, by just calling Gtk.Menu:set_accel_path() on their parent,each menu item of this menu, that contains a label describing itspurpose, automatically gets an accel path assigned. For example, a menu containing menu items “New” and “Exit”, will, afterGtk.Menu:set_accel_path (menu, "<Gnumeric-Sheet>/File"); has beencalled, assign its items the accel paths: "<Gnumeric-Sheet>/File/New"and "<Gnumeric-Sheet>/File/Exit". Assigning accel paths to menu items then enables the user to changetheir accelerators at runtime. More details about accelerator pathsand their default setups can be found at `Gtk.AccelMap:add_entry(). Note thataccel_pathstring will be stored in a GQuark. Therefore,if you pass a static string, you can save some memory by interningit first withg_intern_static_string()`.


get_accel_path()

get_accel_path (self) -> str

Retrieves the accelerator path set on the menu.

  • Returns: the accelerator path set on the menu.

  • Since: 2.14


set_title()

set_title (self, title:str)

Gtk.Menu:set_title has been deprecated since version 3.10 and should not be used in newly-written code. Sets the title string for the menu. The title is displayed when the menu is shown as a tearoffmenu. If title is None, the menu will see if it is attachedto a parent menu item, and if so it will try to use the sametext as that menu item’s label.


get_title()

get_title (self) -> str

Gtk.Menu:get_title has been deprecated since version 3.10 and should not be used in newly-written code. Returns the title of the menu. See Gtk.Menu:set_title().

  • Returns: the title of the menu, or None if the menuhas no title set on it. This string is owned by GTK+and should not be modified or freed.

set_monitor()

set_monitor (self, monitor_num:int)

Informs GTK+ on which monitor a menu should be popped up.See gdk_screen_get_monitor_geometry(). This function should be called from a `Gtk.MenuPositionFuncifthe menu should not appear on the same monitor as the pointer.This information can’t be reliably inferred from the coordinatesreturned by aGtk.MenuPositionFunc`, since, for very long menus,these coordinates may extend beyond the monitor boundaries or eventhe screen boundaries.

  • Since: 2.4

get_monitor()

get_monitor (self) -> int

Retrieves the number of the monitor on which to show the menu.

  • Returns: the number of the monitor on which the menu shouldbe popped up or -1, if no monitor has been set

  • Since: 2.14


get_tearoff_state()

get_tearoff_state (self) -> bool

Gtk.Menu:get_tearoff_state has been deprecated since version 3.10 and should not be used in newly-written code. Returns whether the menu is torn off.See Gtk.Menu:set_tearoff_state().

  • Returns: True if the menu is currently torn off.

set_reserve_toggle_size()

set_reserve_toggle_size (self, reserve_toggle_size:bool)

Sets whether the menu should reserve space for drawing togglesor icons, regardless of their actual presence.

  • Since: 2.18

get_reserve_toggle_size()

get_reserve_toggle_size (self) -> bool

Returns whether the menu reserves space for toggles andicons, regardless of their actual presence.

  • Returns: Whether the menu reserves toggle space

  • Since: 2.18


popdown()

popdown (self)

Removes the menu from the screen.


reposition()

reposition (self)

Repositions the menu according to its position function.


get_active()

get_active (self) -> Gtk.Widget

Returns the selected menu item from the menu. This is used by theGtk.ComboBox.

  • Returns: the Gtk.MenuItem that was last selectedin the menu. If a selection has not yet been made, thefirst menu item is selected.

set_active()

set_active (self, index:int)

Selects the specified menu item within the menu. This is used bythe Gtk.ComboBox and should not be used by anyone else.


set_tearoff_state()

set_tearoff_state (self, torn_off:bool)

Gtk.Menu:set_tearoff_state has been deprecated since version 3.10 and should not be used in newly-written code. Changes the tearoff state of the menu. A menu is normallydisplayed as drop down menu which persists as long as the menu isactive. It can also be displayed as a tearoff menu which persistsuntil it is closed or reattached.


attach_to_widget()

attach_to_widget (self, attach_widget:Gtk.Widget, detacher:Gtk.MenuDetachFunc=None)

Attaches the menu to the widget and provides a callback functionthat will be invoked when the menu calls Gtk.Menu:detach() duringits destruction. If the menu is attached to the widget then it will be destroyedwhen the widget is destroyed, as if it was a child widget.An attached menu will also move between screens correctly if thewidgets moves between screens.


detach()

detach (self)

Detaches the menu from the widget to which it had been attached.This function will call the callback function, detacher, providedwhen the Gtk.Menu:attach_to_widget() function was called.


get_attach_widget()

get_attach_widget (self) -> Gtk.Widget

Returns the Gtk.Widget that the menu is attached to.

  • Returns: the Gtk.Widget that the menu is attached to.

get_for_attach_widget()

get_for_attach_widget (widget:Gtk.Widget) -> list

Returns a list of the menus which are attached to this widget.This list is owned by GTK+ and must not be modified.

  • Returns: the listof menus attached to his widget.

  • Since: 2.6


Example:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

def event(widget, event):
    if event.button == 3:
        menu.popup(None, None, None, None, event.button, event.time)
        menu.show_all()

    return True

window = Gtk.Window()
window.connect("destroy", Gtk.main_quit)

grid = Gtk.Grid()
window.add(grid)

menubar = Gtk.MenuBar()
menubar.set_hexpand(True)
grid.attach(menubar, 0, 0, 1, 1)

menuitem = Gtk.MenuItem(label="File")
menubar.append(menuitem)
menu = Gtk.Menu()
menuitem.set_submenu(menu)
menuitem = Gtk.MenuItem(label="MenuItem")
menu.append(menuitem)

menuitem = Gtk.MenuItem(label="Edit")
menubar.append(menuitem)
menu = Gtk.Menu()
menuitem.set_submenu(menu)
checkmenuitem = Gtk.CheckMenuItem(label="CheckMenuItem")
menu.append(checkmenuitem)

menuitem = Gtk.MenuItem(label="RadioMenuItem")
menubar.append(menuitem)
menu = Gtk.Menu()
menuitem.set_submenu(menu)
radiomenuitem1 = Gtk.RadioMenuItem(label="RadioMenuItem 1")
radiomenuitem1.set_active(True)
menu.append(radiomenuitem1)
radiomenuitem2 = Gtk.RadioMenuItem(label="RadioMenuItem 2", group=radiomenuitem1)
menu.append(radiomenuitem2)

eventbox = Gtk.EventBox()
eventbox.set_size_request(-1, 200)
eventbox.connect("button-release-event", event)
grid.attach(eventbox, 0, 1, 1, 1)

window.show_all()

Gtk.main()

results matching ""

    No results matching ""