1from gi.repository import Gtk 2from gi.repository import GLib 3from gi.repository import Gio 4import sys 5 6 7class MyWindow(Gtk.ApplicationWindow): 8 9 def __init__(self, app): 10 Gtk.Window.__init__(self, title="MenuBar Example", application=app) 11 self.set_default_size(200, 200) 12 13 # action without a state created (name, parameter type) 14 copy_action = Gio.SimpleAction.new("copy", None) 15 # connected with the callback function 16 copy_action.connect("activate", self.copy_callback) 17 # added to the window 18 self.add_action(copy_action) 19 20 # action without a state created (name, parameter type) 21 paste_action = Gio.SimpleAction.new("paste", None) 22 # connected with the callback function 23 paste_action.connect("activate", self.paste_callback) 24 # added to the window 25 self.add_action(paste_action) 26 27 # action with a state created (name, parameter type, initial state) 28 shape_action = Gio.SimpleAction.new_stateful( 29 "shape", GLib.VariantType.new('s'), GLib.Variant.new_string('line')) 30 # connected to the callback function 31 shape_action.connect("activate", self.shape_callback) 32 # added to the window 33 self.add_action(shape_action) 34 35 # action with a state created 36 about_action = Gio.SimpleAction.new("about", None) 37 # action connected to the callback function 38 about_action.connect("activate", self.about_callback) 39 # action added to the application 40 self.add_action(about_action) 41 42 # callback function for copy_action 43 def copy_callback(self, action, parameter): 44 print("\"Copy\" activated") 45 46 # callback function for paste_action 47 def paste_callback(self, action, parameter): 48 print("\"Paste\" activated") 49 50 # callback function for shape_action 51 def shape_callback(self, action, parameter): 52 print("Shape is set to", parameter.get_string()) 53 # Note that we set the state of the action! 54 action.set_state(parameter) 55 56 # callback function for about (see the AboutDialog example) 57 def about_callback(self, action, parameter): 58 # a Gtk.AboutDialog 59 aboutdialog = Gtk.AboutDialog() 60 61 # lists of authors and documenters (will be used later) 62 authors = ["GNOME Documentation Team"] 63 documenters = ["GNOME Documentation Team"] 64 65 # we fill in the aboutdialog 66 aboutdialog.set_program_name("MenuBar Example") 67 aboutdialog.set_copyright( 68 "Copyright \xc2\xa9 2012 GNOME Documentation Team") 69 aboutdialog.set_authors(authors) 70 aboutdialog.set_documenters(documenters) 71 aboutdialog.set_website("http://developer.gnome.org") 72 aboutdialog.set_website_label("GNOME Developer Website") 73 74 # to close the aboutdialog when "close" is clicked we connect the 75 # "response" signal to on_close 76 aboutdialog.connect("response", self.on_close) 77 # show the aboutdialog 78 aboutdialog.show() 79 80 # a callback function to destroy the aboutdialog 81 def on_close(self, action, parameter): 82 action.destroy() 83 84 85class MyApplication(Gtk.Application): 86 87 def __init__(self): 88 Gtk.Application.__init__(self) 89 90 def do_activate(self): 91 win = MyWindow(self) 92 win.show_all() 93 94 def do_startup(self): 95 # FIRST THING TO DO: do_startup() 96 Gtk.Application.do_startup(self) 97 98 # action without a state created 99 new_action = Gio.SimpleAction.new("new", None) 100 # action connected to the callback function 101 new_action.connect("activate", self.new_callback) 102 # action added to the application 103 self.add_action(new_action) 104 105 # action without a state created 106 quit_action = Gio.SimpleAction.new("quit", None) 107 # action connected to the callback function 108 quit_action.connect("activate", self.quit_callback) 109 # action added to the application 110 self.add_action(quit_action) 111 112 # action with a state created 113 state_action = Gio.SimpleAction.new_stateful( 114 "state", GLib.VariantType.new('s'), GLib.Variant.new_string('off')) 115 # action connected to the callback function 116 state_action.connect("activate", self.state_callback) 117 # action added to the application 118 self.add_action(state_action) 119 120 # action with a state created 121 awesome_action = Gio.SimpleAction.new_stateful( 122 "awesome", None, GLib.Variant.new_boolean(False)) 123 # action connected to the callback function 124 awesome_action.connect("activate", self.awesome_callback) 125 # action added to the application 126 self.add_action(awesome_action) 127 128 # a builder to add the UI designed with Glade to the grid: 129 builder = Gtk.Builder() 130 # get the file (if it is there) 131 try: 132 builder.add_from_file("menubar.ui") 133 except: 134 print("file not found") 135 sys.exit() 136 137 # we use the method Gtk.Application.set_menubar(menubar) to add the menubar 138 # and the menu to the application (Note: NOT the window!) 139 self.set_menubar(builder.get_object("menubar")) 140 self.set_app_menu(builder.get_object("appmenu")) 141 142 # callback function for new 143 def new_callback(self, action, parameter): 144 print("You clicked \"New\"") 145 146 # callback function for quit 147 def quit_callback(self, action, parameter): 148 print("You clicked \"Quit\"") 149 sys.exit() 150 151 # callback function for state 152 def state_callback(self, action, parameter): 153 print("State is set to", parameter.get_string()) 154 action.set_state(parameter) 155 156 # callback function for awesome 157 def awesome_callback(self, action, parameter): 158 action.set_state(GLib.Variant.new_boolean(not action.get_state())) 159 if action.get_state().get_boolean() is True: 160 print("You checked \"Awesome\"") 161 else: 162 print("You unchecked \"Awesome\"") 163 164 165app = MyApplication() 166exit_status = app.run(sys.argv) 167sys.exit(exit_status) 168