Gtk.MenuItem
Gtk.MenuItem — The widget used for item in menus
Object Hierarchy:
GObject
╰── GInitiallyUnowned
╰── Gtk.Widget
╰── Gtk.Container
╰── Gtk.Bin
╰── Gtk.MenuItem
├── Gtk.CheckMenuItem
├── Gtk.ImageMenuItem
├── Gtk.SeparatorMenuItem
╰── Gtk.TearoffMenuItem
See also:
Functions:
- new
() -> Gtk.Widget
- new_with_label
(label:str) -> Gtk.Widget
- new_with_mnemonic
(label:str) -> Gtk.Widget
- set_right_justified
(self, right_justified:bool)
- get_right_justified
(self) -> bool
- get_label
(self) -> str
- set_label
(self, label:str)
- get_use_underline
(self) -> bool
- set_use_underline
(self, setting:bool)
- set_submenu
(self, submenu:Gtk.Menu=None)
- get_submenu
(self) -> Gtk.Widget
- set_accel_path
(self, accel_path:str=None)
- get_accel_path
(self) -> str
- select
(self)
- deselect
(self)
- activate
(self)
- toggle_size_request
(self, requisition:int) -> requisition:int
- toggle_size_allocate
(self, allocation:int)
- get_reserve_indicator
(self) -> bool
- set_reserve_indicator
(self, reserve:bool)
Signals:
- “activate”
(menuitem, user_data)
- “activate-item”
(menuitem, user_data)
- “deselect”
(menuitem, user_data)
- “select”
(menuitem, user_data)
- “toggle-size-allocate”
(menuitem, user_data)
Description:
The Gtk.MenuItem
widget and the derived widgets are the only valid children for menus. Their function is to correctly handle highlighting, alignment, events and submenus.
As a Gtk.MenuItem
derives from GtkBin it can hold any valid child widget, although only a few are really useful.
By default, a Gtk.MenuItem
sets a Gtk.AccelLabel as its child.
Gtk.MenuItem
has direct functions to set the label and its mnemonic.
For more advanced label settings, you can fetch the child widget from the Gtk.Bin.
An example for setting accelerator on a MenuItem:
accelgroup = Gtk.AccelGroup()
self.add_accel_group(accelgroup)
menuitem_open = Gtk.MenuItem(label="Open")
menuitem_open.connect('activate', self.on_menu_open)
menuitem_open.add_accelerator("activate",
accelgroup,
Gdk.keyval_from_name("o"),
Gdk.ModifierType.CONTROL_MASK,
Gtk.AccelFlags.VISIBLE)
Gtk.MenuItem as Gtk.Buildable:
The Gtk.MenuItem implementation of the Gtk.Buildable interface supports adding a submenu by specifying submenu
as the type
attribute of a \
- An example of UI definition fragment with submenus:
<object class="GtkMenuItem">
<child type="submenu">
<object class="GtkMenu"/>
</child>
</object>
Function Details:
new()
new () -> Gtk.Widget
Creates a new Gtk.MenuItem
.
- Returns: the newly created Gtk.MenuItem
new_with_label()
new_with_label (label:str) -> Gtk.Widget
Creates a new Gtk.MenuItem
whose child is a Gtk.Label
.
- Returns: the newly created Gtk.MenuItem
new_with_mnemonic()
new_with_mnemonic (label:str) -> Gtk.Widget
Creates a new Gtk.MenuItem
containing a label.
The label will be created using `Gtk.Label:new_with_mnemonic
(),so underscores in
label` indicate the mnemonic for the menu item.
- Returns: a new Gtk.MenuItem
set_right_justified()
set_right_justified (self, right_justified:bool)
Gtk.MenuItem:set_right_justified
has been deprecated since version 3.2 and should not be used in newly-written code.
If you insist on using it, use Gtk.Widget:set_hexpand`()` and
Gtk.Widget:set_halign()
.
Sets whether the menu item appears justified at the rightside of a menu bar. This was traditionally done for “Help”menu items, but is now considered a bad idea. (If the widgetlayout is reversed for a right-to-left language like Hebrewor Arabic, right-justified-menu-items appear at the left.)
get_right_justified()
get_right_justified (self) -> bool
Gtk.MenuItem:get_right_justified
has been deprecated since version 3.2 and should not be used in newly-written code.
See Gtk.MenuItem:set_right_justified()
Gets whether the menu item appears justified at the rightside of the menu bar.
- Returns:
True
if the menu item will appear at thefar right if added to a menu bar.
get_label()
get_label (self) -> str
Sets text
on the menu_item
label
Returns: The text in the
menu_item
label. This is the internalstring used by the label, and must not be modified.Since: 2.16
set_label()
set_label (self, label:str)
Sets text
on the menu_item
label
- Since: 2.16
get_use_underline()
get_use_underline (self) -> bool
Checks if an underline in the text indicates the next charactershould be used for the mnemonic accelerator key.
Returns:
True
if an embedded underline in the labelindicates the mnemonic accelerator key.Since: 2.16
set_use_underline()
set_use_underline (self, setting:bool)
If true, an underline in the text indicates the next charactershould be used for the mnemonic accelerator key.
- Since: 2.16
set_submenu()
set_submenu (self, submenu:Gtk.Menu=None)
Sets or replaces the menu item’s submenu, or removes it when a None
submenu is passed.
get_submenu()
get_submenu (self) -> Gtk.Widget
Gets the submenu underneath this menu item, if any.See Gtk.MenuItem:set_submenu()
.
- Returns:
submenu for this menu item, or
None
if none.
set_accel_path()
set_accel_path (self, accel_path:str=None)
Set the accelerator path on menu_item
, through which runtimechanges of the menu item’s accelerator caused by the user can beidentified and saved to persistent storage (see Gtk.AccelMap:save`()`on this). To set up a default accelerator for this menu item, call
Gtk.AccelMap:add_entry()
with the same accel_path
. See alsoGtk.AccelMap:add_entry`()` on the specifics of accelerator paths,and
Gtk.Menu:set_accel_path()
for a more convenient variant ofthis function.
This function is basically a convenience wrapper that handlescalling Gtk.Widget:set_accel_path`()` with the appropriate acceleratorgroup for the menu item.
Note that you do need to set an accelerator on the parent menu with
Gtk.Menu:set_accel_group()
for this to work.
Note that accel_path
string will be stored in a GQuark.Therefore, if you pass a static string, you can save some memoryby interning it first with g_intern_static_string()
.
get_accel_path()
get_accel_path (self) -> str
Retrieve the accelerator path that was previously set on menu_item
.
See Gtk.MenuItem:set_accel_path()
for details.
Returns: the accelerator path corresponding to this menuitem’s functionality, or
None
if not setSince: 2.14
select()
select (self)
Emits the “select” signal on the given item.
deselect()
deselect (self)
Emits the “deselect” signal on the given item.
activate()
activate (self)
Emits the “activate” signal on the given item
toggle_size_request()
toggle_size_request (self, requisition:int) -> requisition:int
Emits the “toggle-size-request” signal on the given item.
toggle_size_allocate()
toggle_size_allocate (self, allocation:int)
Emits the “toggle-size-allocate” signal on the given item.
get_reserve_indicator()
get_reserve_indicator (self) -> bool
Returns whether the menu_item
reserves space forthe submenu indicator, regardless if it has a submenuor not.
Returns:
True
ifmenu_item
always reserves space for thesubmenu indicatorSince: 3.0
set_reserve_indicator()
set_reserve_indicator (self, reserve:bool)
Sets whether the menu_item
should reserve space forthe submenu indicator, regardless if it actually hasa submenu or not.
There should be little need for applications to callthis functions.
- Since: 3.0
Example:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="MenuItem Demo")
self.set_size_request(400, 300)
grid = Gtk.Grid()
self.add(grid)
menubar = Gtk.MenuBar()
menubar.set_hexpand(True)
grid.attach(menubar, 0, 0, 1, 1)
menuitem_file = Gtk.MenuItem(label="File")
menubar.append(menuitem_file)
submenu_file = Gtk.Menu()
menuitem_file.set_submenu(submenu_file)
menuitem_open = Gtk.MenuItem(label="Open")
submenu_file.append(menuitem_open)
menuitem_open.connect('activate', self.on_menu_open)
menuitem_quit = Gtk.MenuItem(label="Quit")
submenu_file.append(menuitem_quit)
menuitem_quit.connect('activate', self.on_menu_quit)
menuitem_edit = Gtk.MenuItem(label="Edit")
menubar.append(menuitem_edit)
def on_menu_open(self, widget):
print("add file open dialog")
def on_menu_quit(self, widget):
Gtk.main_quit()
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()