class Main: """ The Main class is responsable for loading the user interface from the UI_FILE using Glade and connecting the actions to all the respective callback. """ UI_FILE = 'window.glade' DEFAULT_GRID_SIZE = 5 MAX_GRID_SIZE = 10 MIN_GRID_SIZE = 1 def action_solve_callback(self, action): """ Callback connected to action-solve, invoked when the solve button is toggled. """ active = self.togglebutton_solve.get_active() self.grid.set_solve(active) def action_scramble_callback(self, action): """ Callback connected to action-scramble, invoked when the scramble button is clicked. """ # just forward the call to the grid self.grid.scramble() def action_clear_callback(self, action): """ Callback connected to action-clear, invoked when the clear button is clicked. """ # Note that resetting all the button to the off status might be a # better solution than creating a new Grid object self.box.remove(self.grid) self.grid = Grid(self.grid_size) active = self.togglebutton_solve.get_active() self.grid.set_solve(active) self.grid.show_all() self.box.add(self.grid) def action_expand_callback(self, action): """ Callback connected to action-expand, invoked when the expand button is clicked. """ self.grid_size += 1 self.box.remove(self.grid) self.grid = Grid(self.grid_size) active = self.togglebutton_solve.get_active() self.grid.set_solve(active) self.grid.show_all() self.box.add(self.grid) self.button_contract.set_sensitive(True) if self.grid_size == self.MAX_GRID_SIZE: self.button_expand.set_sensitive(False) def action_contract_callback(self, action): """ Callback connected to action-contract, invoked when the contract button is clicked. """ self.grid_size -= 1 self.box.remove(self.grid) self.grid = Grid(self.grid_size) active = self.togglebutton_solve.get_active() self.grid.set_solve(active) self.grid.show_all() self.box.add(self.grid) self.button_expand.set_sensitive(True) if self.grid_size == self.MIN_GRID_SIZE: self.button_contract.set_sensitive(False) def __init__(self): self.grid_size = self.DEFAULT_GRID_SIZE builder = Gtk.Builder() builder.add_from_file(self.UI_FILE) self.togglebutton_solve = builder.get_object('togglebutton-solve') self.button_expand = builder.get_object('button-expand') self.button_contract = builder.get_object('button-contract') self.box = builder.get_object('box') self.grid = Grid(self.grid_size) self.box.add(self.grid) action_solve = builder.get_object('action-solve') action_solve.connect('activate', self.action_solve_callback) action_scramble = builder.get_object('action-scramble') action_scramble.connect('activate', self.action_scramble_callback) action_clear = builder.get_object('action-clear') action_clear.connect('activate', self.action_clear_callback) action_expand = builder.get_object('action-expand') action_expand.connect('activate', self.action_expand_callback) action_contract = builder.get_object('action-contract') action_contract.connect('activate', self.action_contract_callback) window = builder.get_object('window') window.connect_after('destroy', Gtk.main_quit) window.show_all() window.set_size_request(400, 400) Gtk.main()
#!/usr/bin/python try: from gi.repository import Gtk, Gdk except ImportError: raise Exception('unable to import required module') from grid import Grid grid = Grid() grid.connect('delete-event', Gtk.main_quit) grid.show_all() Gtk.main()