def get_and_set_board(self): self.boardextractor = BoardExtractor(self.frameslist[HomePage].selectedimagepath) self.boardextractor.preprocess_image() self.boardextractor.detect_and_crop_grid() boardcells = self.boardextractor.create_image_grid() self.recognizeandconstructobj = ConstructGrid(boardcells, self.modeltype) board = self.recognizeandconstructobj.constructgrid() self.board = deepcopy(board) self.sudokugameobj = SudokuGame(self.board) F = SudokuUI frame = F(self.container, self, self.sudokugameobj) frame.grid(row=0, column=0, sticky="nsew") self.frameslist[F] = frame
class MainUI(Tk): def __init__(self, modeltype): Tk.__init__(self) self.resizable(width=False, height=False) try: os.makedirs("CleanedBoardCells") os.makedirs("StagesImages") os.makedirs("BoardCells") except: pass self.modeltype = modeltype self.board = None self.solutiongrid = None self.container = Frame(self) self.recognizeandconstructobj = None self.boardextractor = None self.container.pack(fill=BOTH, expand=True) self.frameslist = {} for F in (HomePage, StagesFrame): frame = F(self.container, self) frame.grid(row=0, column=0, sticky="nsew") self.frameslist[F] = frame self.show_frame(HomePage) self.title("AI Sudoku") def get_and_set_board(self): self.boardextractor = BoardExtractor( self.frameslist[HomePage].selectedimagepath) self.boardextractor.preprocess_image() self.boardextractor.detect_and_crop_grid() boardcells = self.boardextractor.create_image_grid() self.recognizeandconstructobj = ConstructGrid(boardcells, self.modeltype) board = self.recognizeandconstructobj.constructgrid() self.board = deepcopy(board) self.sudokugameobj = SudokuGame(self.board) F = SudokuUI frame = F(self.container, self, self.sudokugameobj) frame.grid(row=0, column=0, sticky="nsew") self.frameslist[F] = frame def getsolngrid(self): self.solutiongrid = None solverobj = Solver() tmp = deepcopy(self.board) #solverobj.print_board(tmp) if not solverobj.checkvalidpuzzle(tmp): messagebox.showerror( "Invalid Puzzle", "The puzzle board is invalid, please rectify the wrong entries and try again" ) return False if not solverobj.solve_sudoku(tmp): messagebox.showerror("No Solution!", "This puzzle has no solution") self.show_frame(SudokuUI) else: self.solutiongrid = deepcopy(tmp) return True def show_frame(self, cont): frame = self.frameslist[cont] frame.tkraise() def cleanup(self): try: rmtree("BoardCells/") except: pass try: rmtree("CleanedBoardCells/") except: pass try: rmtree("StagesImages/") except: pass
class MainUI(Tk): def __init__(self, modeltype): Tk.__init__(self) self.resizable(width=False, height=False) # Create the folders if they don't already exist try: os.makedirs("CleanedBoardCells") os.makedirs("StagesImages") os.makedirs("BoardCells") except: pass # Modeltype CNN or KNN self.modeltype = modeltype # Variables for the Sudoku Board and Solution respectively self.board = None self.solutiongrid = None # The main frame self.container = Frame(self) # An object for the RecognizeAndConstructBoard Class self.recognizeandconstructobj = None # An object for the Boardextractor Class self.boardextractor = None self.container.pack(fill=BOTH, expand=True) # A Dictionary containing all the frames self.frameslist = {} # Create the HomePage Frame and StagesFrame Frame and append to frames list for F in (HomePage, StagesFrame): frame = F(self.container, self) frame.grid(row=0, column=0, sticky="nsew") self.frameslist[F] = frame # Show the first frame ie.. the homepage self.show_frame(HomePage) self.title("AI Sudoku") '''This function is called when the user presses the next button in the Home Page. As soon as the path to the image is obtained, this function initializes an object of BoardExtractor, calls it's functions to get a 2D array of the board cell images, then creates an object of ConstructGrid Class passing the 2D array to it, then the recognized sudoku grid returned by the constructgrid() function is stored in the board variable. Then the Sudoku Game Object is created, the SudokuUI Frame is created and added to the frameslist. ''' def get_and_set_board(self): self.boardextractor = BoardExtractor( self.frameslist[HomePage].selectedimagepath) self.boardextractor.preprocess_image() self.boardextractor.detect_and_crop_grid() boardcells = self.boardextractor.create_image_grid() self.recognizeandconstructobj = ConstructGrid(boardcells, self.modeltype) board = self.recognizeandconstructobj.constructgrid() self.board = deepcopy(board) self.sudokugameobj = SudokuGame(self.board) F = SudokuUI frame = F(self.container, self, self.sudokugameobj) frame.grid(row=0, column=0, sticky="nsew") self.frameslist[F] = frame '''This function is called when the reveal button is pressed in the SudokuUI Frame. This function creates an object of the Sudoku Solver's Solver class and stored the solved grid returned by it's solve_sudoku() function in the solutiongrid variable''' def getsolngrid(self): self.solutiongrid = None solverobj = Solver() tmp = deepcopy(self.board) #solverobj.print_board(tmp) if not solverobj.checkvalidpuzzle(tmp): messagebox.showerror( "Invalid Puzzle", "The puzzle board is invalid, please rectify the wrong entries and try again" ) return False if not solverobj.solve_sudoku(tmp): messagebox.showerror("No Solution!", "This puzzle has no solution") self.show_frame(SudokuUI) else: self.solutiongrid = deepcopy(tmp) return True '''This function shows a particular frame by raising it''' def show_frame(self, cont): frame = self.frameslist[cont] frame.tkraise() '''This function cleans up by deleting the created directories''' def cleanup(self): try: rmtree("BoardCells/") except: pass try: rmtree("CleanedBoardCells/") except: pass try: rmtree("StagesImages/") except: pass