1from gi.repository import Gtk 2from gi.repository import Gdk 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="Toolbar Example", application=app) 11 self.set_default_size(400, 200) 12 13 # a grid to attach the toolbar (see below) 14 grid = Gtk.Grid() 15 self.add(grid) 16 # we have to show the grid (and therefore the toolbar) with show(), 17 # as show_all() would show also the buttons in the toolbar that we want to 18 # be hidden (such as the leave_fullscreen button) 19 grid.show() 20 21 # a builder to add the UI designed with Glade to the grid: 22 builder = Gtk.Builder() 23 # get the file (if it is there) 24 try: 25 builder.add_from_file("toolbar_builder.ui") 26 except: 27 print("file not found") 28 sys.exit() 29 # and attach it to the grid 30 grid.attach(builder.get_object("toolbar"), 0, 0, 1, 1) 31 32 # two buttons that will be used later in a method 33 self.fullscreen_button = builder.get_object("fullscreen_button") 34 self.leave_fullscreen_button = builder.get_object( 35 "leave_fullscreen_button") 36 37 # create the actions that control the window, connect their signal to a 38 # callback method (see below), add the action to the window: 39 40 # undo 41 undo_action = Gio.SimpleAction.new("undo", None) 42 undo_action.connect("activate", self.undo_callback) 43 self.add_action(undo_action) 44 45 # and fullscreen 46 fullscreen_action = Gio.SimpleAction.new("fullscreen", None) 47 fullscreen_action.connect("activate", self.fullscreen_callback) 48 self.add_action(fullscreen_action) 49 50 # callback for undo 51 def undo_callback(self, action, parameter): 52 print("You clicked \"Undo\".") 53 54 # callback for fullscreen 55 def fullscreen_callback(self, action, parameter): 56 # check if the state is the same as Gdk.WindowState.FULLSCREEN, which 57 # is a bit flag 58 is_fullscreen = self.get_window().get_state( 59 ) & Gdk.WindowState.FULLSCREEN != 0 60 if is_fullscreen: 61 self.unfullscreen() 62 self.leave_fullscreen_button.hide() 63 self.fullscreen_button.show() 64 else: 65 self.fullscreen() 66 self.fullscreen_button.hide() 67 self.leave_fullscreen_button.show() 68 69 70class MyApplication(Gtk.Application): 71 72 def __init__(self): 73 Gtk.Application.__init__(self) 74 75 def do_activate(self): 76 win = MyWindow(self) 77 # show the window - with show() not show_all() because that would show also 78 # the leave_fullscreen button 79 win.show() 80 81 def do_startup(self): 82 Gtk.Application.do_startup(self) 83 84 # actions that control the application: create, connect their signal to a 85 # callback method (see below), add the action to the application 86 87 # new 88 new_action = Gio.SimpleAction.new("new", None) 89 new_action.connect("activate", self.new_callback) 90 app.add_action(new_action) 91 92 # open 93 open_action = Gio.SimpleAction.new("open", None) 94 open_action.connect("activate", self.open_callback) 95 app.add_action(open_action) 96 97 # callback for new 98 def new_callback(self, action, parameter): 99 print("You clicked \"New\".") 100 101 # callback for open 102 def open_callback(self, action, parameter): 103 print("You clicked \"Open\".") 104 105app = MyApplication() 106exit_status = app.run(sys.argv) 107sys.exit(exit_status) 108