Gtkmenushell inheritance Relationship Methods Virtual Methods Properties Signals Gtkmenubar inheritance Relationship Methods Properties Gtkmenu inheritance Relationship Methods Prope Rties Signals Example One menubar two checkmenu three imagemenu four menupopup five
Before introducing Gtk.menu, you must first understand the Gtk.menushell abstract class, Gtk.menu and Gtk.menubar implement the Gtk.menushell abstract class interface Gtk.menushell Inheritance Relationship
Gtk.menushell is a direct subclass of Gtk.container, Gtk.menushell is an abstract class
Methods
method Modifier |
method name and parameter |
|
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
|
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
Name |
Type |
Flags | Short
Description |
Take-focus |
bool |
r/w/en |
A Boolean that determines whether the menu grabs the keyboard focus |
Signals
Name | Short
Description |
Activate-current |
An action signal which 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 was emitted when a menu shell is deactivated. |
Insert |
The:: Insert signal is emitted when a new gtk.menuitem was 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 was emitted when a selection has been completed within a menu shell. |
Gtk.menubar
Inheritance Relationship
Methods
method Modifier |
method name and parameter |
Static |
New () |
Static |
New_from_model (model) |
|
Get_child_pack_direction () |
|
Get_pack_direction () |
|
Set_child_pack_direction (Child_pack_dir) |
|
Set_pack_direction (Pack_dir) |
Properties
Name |
Type |
Flags | Short
Description |
Child-pack-direction |
Gtk.packdirection |
r/w/en |
The child pack direction of the MenuBar |
Pack-direction |
Gtk.packdirection |
r/w/en |
The pack direction of the MenuBar |
Gtk.menu
Inheritance Relationship
Methods
method Modifier |
method name and parameter |
Static |
Get_for_attach_widget (widget) |
Static |
New () |
Static |
New_from_model (model) |
|
Attach (Child, Left_attach, Right_attach, Top_attach, Bottom_attach) |
|
Attach_to_widget (Attach_widget, Detacher) |
|
Detach () |
|
Get_accel_group () |
|
Get_accel_path () |
|
Get_active () |
|
Get_attach_widget () |
|
Get_monitor () |
|
Get_reserve_toggle_size () |
|
Get_tearoff_state () |
|
Get_title () |
|
Popdown () |
|
Popup (Parent_menu_shell, Parent_menu_item, func, Data, button, Activate_time) |
|
Popup_for_device (device, Parent_menu_shell, Parent_menu_item, func, Data, button, Activate_time) |
|
Reorder_child (Child, position) |
|
Reposition () |
|
Set_accel_group (Accel_group) |
|
Set_accel_path (Accel_path) |
|
Set_active (Index) |
|
Set_monitor (Monitor_num) |
|
Set_reserve_toggle_size (Reserve_toggle_size) |
|
Set_screen (screen) |
|
Set_tearoff_state (Torn_off) |
|
Set_title (title) |
Properties
Name |
Type |
Flags | Short
Description |
Accel-group |
Gtk.accelgroup |
r/w |
The Accel Group Holding accelerators for the menu |
Accel-path |
Str |
r/w |
An Accel path used to conveniently construct accel paths of child items |
Active |
Int |
r/w |
The currently selected menu item |
Attach-widget |
Gtk.widget |
r/w |
The widget the menu is attached to |
Monitor |
Int |
r/w/en |
The monitor the menu is popped up on |
Reserve-toggle-size |
bool |
r/w/en |
A Boolean that indicates whether the menu reserves spaces for toggles and icons |
Tearoff-state |
bool |
d/r/w |
A Boolean that indicates whether the ' menu is Torn-off deprecated |
Tearoff-title |
Str |
d/r/w |
A title, May, displayed by the window Manager as this menu is Torn-off deprecated |
Signals
Name | Short
Description |
Move-scroll |
|
Example
I. MenuBar
Code:
#!/usr/bin/env Python3 # Created by Xiaosanyu in 16/7/4 # section 095 TITLE = "MenuBar" DESCRIPTION = "" "" Gtk.menubar
is a subclass of Gtk.menushell which contains one or more gtk.menuitems.
The result is a standard menu bar which can hold many menu items.
"" "Import gi gi.require_version (' Gtk ', ' 3.0 ') from gi.repository import GTK, GdK class Menubarwindow (Gtk.window): def __init__ (self): gtk.window.__init__ (Self, title= "MenuBar Example") self.set_border_width (3) SE lf.set_size_request MB = Gtk.menubar () Filemenu = Gtk.menu () Filem = Gtk.menuitem ("File" ) Filem.set_submenu (filemenu) exit = Gtk.menuitem ("Exit") Exit.connect ("Activate", Self.on_destroy ) Filemenu.append (exit) mb.append (filem) VBox = Gtk.vbox (False, 2) vbox.pack_start (MB, Fal SE, False, 0) Self.add (vbox) def on_destroy (self, widget): Self.destroy () gtk.main_quit () d EF Main(): win = Menubarwindow () win.connect ("Delete-event", Gtk.main_quit) Win.show_all () Gtk.main () if __name
__ = = "__main__": Main ()
Code resolution two. Checkmenu
Code
#!/usr/bin/env Python3 # Created by Xiaosanyu in 16/7/4 # section 091 TITLE = "Checkmenu" DESCRIPTION = "" "A gtk.checkmen Uitem is a menu item this maintains the state of a Boolean value in addition to a gtk.menuitem usual role in activating AP
Plication code.
"" "Import gi gi.require_version (' Gtk ', ' 3.0 ') from gi.repository import GTK, GdK class Checkmenuwindow (Gtk.window):
def __init__ (self): gtk.window.__init__ (Self, title= "Checkmenu Example") self.set_border_width (3) self.set_size_request MB = Gtk.menubar () Filemenu = Gtk.menu () Filem = Gtk.menuitem ("F Ile ") Filem.set_submenu (filemenu) Viewmenu = Gtk.menu () view = Gtk.menuitem (" View ") view.s Et_submenu (viewmenu) stat = Gtk.checkmenuitem ("View Statusbar") stat.set_active (True) stat.connect ("Activate", Self.on_status_view) viewmenu.append (stat) exit = Gtk.menuitem ("Exit") Exit.connect (" Activate", Self.on_destroy) filemenu.append (exit) mb.append (Filem) mb.append (view) Self.statusbar = Gtk.statusbar () self.statusbar.push (1, "Ready") VBox = Gtk.vbox (False, 2) vbox.pack_start (MB, Fa LSE, FALSE, 0) Vbox.pack_start (Gtk.label (), True, False, 0) Vbox.pack_start (Self.statusbar, False, False, 0) Self.add (vbox) def on_status_view (self, widget): If Widget.get_active (): SELF.STATUSBA
R.show () else:self.statusbar.hide () def on_destroy (self, widget): Self.destroy () Gtk.main_quit () def main (): win = Checkmenuwindow () win.connect ("Delete-event", Gtk.main_quit) Win.show_all
() Gtk.main () if __name__ = = "__main__": Main ()
three. ImageMenu
Code
#!/usr/bin/env Python3 # Created by Xiaosanyu in 16/7/4 # section 092 TITLE = "ImageMenu" DESCRIPTION = "" "A gtk.imagemen
Uitem is a menu item which has of the icon next to the text label.
"" "Import gi gi.require_version (' Gtk ', ' 3.0 ') from gi.repository import GTK, GdK class Imagemenuwindow (Gtk.window):
def __init__ (self): gtk.window.__init__ (Self, title= "ImageMenu Example") self.set_border_width (3) self.set_size_request MB = Gtk.menubar () Filemenu = Gtk.menu () Filem = Gtk.menuitem ("_ File ") Filem.set_submenu (filemenu) AGR = Gtk.accelgroup () self.add_accel_group (AGR) # Newi = Gtk.imagemenuitem (gtk.stock_new, AGR) # This will have a shortcut key prompt, no icon # Newi = Gtk.ImageMenuItem.new_with_label ("NEW") #这个会有快 Czech key prompt, no icon Newi = gtk.imagemenuitem () box = Gtk.box () box.add (Gtk.Image.new_from_icon_name ("Documen T-new ", Gtk.IconSize.MENU)") label = Gtk.accellabel ("new") Label.set_xalign (0) # You must use the following two sentences to display the shortcut key Label.set_accel_widget (NEWI) box.pack_end (label, True, True, 0) Newi.add (Box) newi.connect ("Activate", Self.menu_select, "New") key, mod = Gtk.accelerator_parse ("< Control>n ") newi.add_accelerator (" Activate ", AGR, Key, MOD, Gtk.AccelFlags.VISIBLE ) Filemenu.append (newi) Openm = Gtk.imagemenuitem () box = Gtk.box () box.add (Gtk.Image.new
_from_icon_name ("Document-open", Gtk.IconSize.MENU)) label = Gtk.accellabel ("open") label.set_xalign (0) # You must use the following two sentences to display the shortcut key Label.set_accel_widget (OPENM) box.pack_end (label, True, True, 0) key, MoD
= Gtk.accelerator_parse ("<control>o") openm.add_accelerator ("Activate", AGR, Key,
MoD, Gtk.AccelFlags.VISIBLE) openm.add (box) openm.connect ("Activate", Self.menu_select, "Open") Filemenu.append (OpenmSep = Gtk.separatormenuitem () filemenu.append (SEP) exit = Gtk.imagemenuitem () box = GTK . Box () Box.add (Gtk.Image.new_from_icon_name ("Application-exit", Gtk.IconSize.MENU)) label = Gtk.accellabel ( "Quit") label.set_xalign (0) # You must use the following two sentences to display the shortcut key Label.set_accel_widget (exit) Box.pack_end (LA Bel, True, True, 0) exit.add (box) key, mod = Gtk.accelerator_parse ("<control>q") Exit.add_ac Celerator ("Activate", AGR, Key, mod, Gtk.AccelFlags.VISIBLE) exit.connect ("Activate", Self.on_destroy) Filemenu.append (exit) mb.append (filem) VBox = Gtk.vbox (False, 2) VBOX.P Ack_start (MB, false, False, 0) Self.add (vbox) @staticmethod def menu_select (widget, name): Print ( Name, "select") def on_destroy (self, widget): Self.destroy () Gtk.main_quit () def main (): win = Imagemenuwindow () WIn.connect ("Delete-event", Gtk.main_quit) Win.show_all () Gtk.main () If __name__ = "__main__": Main ()
four. Menupopup
Code:
#!/usr/bin/env Python3 # Created by Xiaosanyu in 16/6/14 # section 093 TITLE = "Menupopup" DESCRIPTION = "" "Menu.popup ()
Displays a menu and makes it available for selection. "" "Import gi gi.require_version (' Gtk ', ' 3.0 ') from gi.repository import Gtk, gdk, Gio items = [" EditCopy "," EditPaste ", "Editsomething"] class Menupopupwindow (Gtk.window): def __init__ (self): gtk.window.__init__ (Self, title= "Me Nupopup Demo ") self.set_border_width (self.set_size_request) VBox = Gtk.vbox (spacing=6 Self.add (vbox) button = Gtk.button ("button") Vbox.pack_start (button, False, False, 0) m ENU = Gtk.Menu.new () for item in Items:item = Gtk.menuitem (label=item) item.connect ("Acti Vate ", self.on_menu_selected) Item.show () menu.append (item) button.connect (" Button_press_e Vent ", Self.my_popup_handler, menu) @staticmethod def my_popup_handler (Windows, evenT, menu): if Event.type = = Gdk.EventType.BUTTON_PRESS:if Event.button = = Gdk.button_primary: Menu.popup (None, none, none, none, Event.button, Event.time) return Tru E return False @staticmethod def on_menu_selected (widget, *args): Print ("menu item" + widget.ge
T_label () + "was selected") def Main (): win = Menupopupwindow () win.connect ("Delete-event", Gtk.main_quit)
Win.show_all () Gtk.main () if __name__ = = "__main__": Main ()
Five
Code:
#!/usr/bin/env Python3 # Created by Xiaosanyu in 16/6/16 # section 094 TITLE = "Menus" DESCRIPTION = "" Menus UI "" Impo RT GI Gi.require_version (' Gtk ', ' 3.0 ') from gi.repository import Gtk, GdK ui_info = "" "<ui> <menubar name= ' M Enubar ' > <menu action= ' filemenu ' > <menu action= ' filenew ' > <menuitem action= ' Filenewsta Ndard '/> <menuitem action= ' filenewfoo '/> <menuitem ' action= ' Filenewgoo '/> </menu > <separator/> <menuitem action= ' filequit '/> </menu> <menu action= ' Editmenu ' > <menuitem action= ' editcopy '/> <menuitem action= ' editpaste '/> <menuitem action= ' Edi
Tsomething '/> </menu> <menu action= ' choicesmenu ' > <menuitem action= ' choiceone '/> <menuitem action= ' Choicetwo '/>, <separator/> <menuitem action= ' choicethree '/> </menu
> </menubar> <toolbar name= ' toolbar ' > <toolitem action= ' filenewstandard '/> <toolitem ' action= ' filequit '/> </toolbar> <popup Nam