Exemplo n.º 1
0
Arquivo: gui.py Projeto: sp5pe/tiles
    def show_board(self):

        self.board_frame = Frame(self.puz_frame, bg="gray", highlightthickness=0, pady=20)
        self.board_frame.pack()
        board = self.board_tile.get_tile_grid()

        rows = len(board)
        cols = len(max(board, key=len))

        board_frame_array = [[" "] * cols for _ in range(rows)]

        for x in range(rows):
            for y in range(cols):
                if isinstance(board[x][y], Cell):
                    label = Label(
                        self.board_frame, text=board[x][y].char, bd=1, font=("Helvetica", 20), relief=RAISED, width=2
                    )
                    label.grid(row=x, column=y)
                    board_frame_array[x][y] = label

        self.board_tile.frame = board_frame_array

        width = self.board_frame.winfo_reqwidth()
        self.board_frame.update()
        print(width)

        self.sols_frame = Frame(self.puz_frame, highlightthickness=0, height=1000, width=500)
        self.sols_frame.pack(fill=X)

        self.scrollbar_sols = AutoScrollbar(self.sols_frame)
        self.scrollbar_sols.grid(row=0, column=1, sticky=N + S)

        self.canvas_sol_holder = Canvas(self.sols_frame, bg="gray", height=1000)
        self.canvas_sol_holder.configure(highlightthickness=0, yscrollcommand=self.scrollbar_sols.set)
        self.canvas_sol_holder.grid(row=0, column=0, sticky=N + S + E + W)
        self.canvas_sol_holder.pack_propagate(FALSE)

        self.scrollbar_sols.config(command=self.canvas_sol_holder.yview)

        self.sols_frame.grid_rowconfigure(0, weight=1)
        self.sols_frame.grid_columnconfigure(0, weight=1)

        self.solutions_frame = Frame(self.canvas_sol_holder, bg="gray", height=1000)
        self.solutions_frame.rowconfigure(1, weight=1)
        self.solutions_frame.columnconfigure(1, weight=1)

        self.canvas_sol_holder.create_window(0, 0, anchor=NW, window=self.solutions_frame)
        self.solutions_frame.update_idletasks()
        self.canvas_sol_holder.config(scrollregion=self.canvas_sol_holder.bbox("all"))
Exemplo n.º 2
0
Arquivo: gui.py Projeto: sp5pe/tiles
    def show_tiles(self):

        self.scrollbar_tiles = AutoScrollbar(self.tiles_frame)
        self.scrollbar_tiles.grid(row=0, column=1, sticky=N + S)

        self.holder = Canvas(self.tiles_frame, bg="#34495e")
        self.holder.configure(highlightthickness=0, yscrollcommand=self.scrollbar_tiles.set)
        self.holder.grid(row=0, column=0, sticky=N + S + E + W)

        self.scrollbar_tiles.config(command=self.holder.yview)
        self.holder.configure(scrollregion=self.holder.bbox("all"))

        self.tiles_frame.grid_rowconfigure(0, weight=1)
        self.tiles_frame.grid_columnconfigure(0, weight=1)

        frame = Frame(self.holder, bg="#34495e", padx=30, pady=10)
        frame.rowconfigure(1, weight=1)
        frame.columnconfigure(1, weight=1)

        for t in self.tiles:
            print("generating tile...")
            tile_grid = t.get_tile_grid()
            tile_frame = Frame(frame, bg="#34495e")
            tile_frame.pack(fill=Y, padx=10, pady=10)

            rows = len(tile_grid)
            cols = len(max(tile_grid, key=len))

            tile_frame_array = [[" "] * cols for _ in range(rows)]

            for x in range(rows):
                for y in range(cols):
                    if isinstance(tile_grid[x][y], Cell):
                        label = Label(tile_frame, text=tile_grid[x][y].char, bd=1, width=2, bg=t.color)
                        label.grid(row=x, column=y)
                        tile_frame_array[x][y] = label

            t.frame = tile_frame_array
            self.tile_frames.append(tile_frame)

        self.holder.create_window(0, 0, anchor=NW, window=frame)

        frame.update_idletasks()

        self.holder.config(scrollregion=self.holder.bbox("all"))

        self.show_board()
Exemplo n.º 3
0
Arquivo: gui.py Projeto: sp5pe/tiles
class Puzzle(Tk):
    """gui class for puzzle"""

    def __init__(self):
        self.root = Tk.__init__(self)

        self.board_tile = None
        self.tiles = []
        self.filename = StringVar(value="filename:   -")

        self.tile_frames = []
        self.board_frame = None
        self.holder = None
        self.scrollbar_tiles = None
        self.solutions = StringVar(value="-")
        self.solutions_frame = None
        self.sols_frame = None

        self.canvas_sol_holder = None
        self.scrollbar_sols = None

        self.top = Frame(self.root, width=800, height=50)
        self.top.pack(fill=BOTH)
        self.top.pack_propagate(FALSE)

        self.tiles_frame = Frame(self.root, width=175, height=1000, bg="#34495e")
        self.tiles_frame.pack(anchor=W, side=LEFT)
        self.tiles_frame.grid_propagate(FALSE)

        self.puz_frame = Frame(self.root, width=625, height=550, bg="gray")
        self.puz_frame.pack(anchor=W, side=LEFT)
        self.puz_frame.pack_propagate(FALSE)

        self.init_board()
        self.init_ui()

    def init_board(self):

        add_width_pack(self.top, 5, True)

        load = Button(self.top, text="load puzzle", command=self.parse_file)
        load.pack(anchor=W, side=LEFT)

        add_width_pack(self.top, 5, True)

        solve = Button(self.top, text="solve puzzle", command=self.solve_puzzle)
        solve.pack(anchor=W, side=LEFT)

        file_label = Label(self.top, text="filename: -", textvariable=self.filename)
        file_label.configure(padx=10)
        file_label.pack(anchor=E, side=RIGHT)

        add_width_pack(self.top, 10, False)

        Label(self.top, textvariable=self.solutions).pack(anchor=E, side=RIGHT)
        Label(self.top, text="solutions: ").pack(anchor=E, side=RIGHT)

    def init_ui(self):
        self.title("tiling puzzle solver")
        self.geometry("{}x{}".format(700, 600))
        # self.resizable(height=FALSE, width=FALSE)
        # self.configure(bg='gray')

    def show_tiles(self):

        self.scrollbar_tiles = AutoScrollbar(self.tiles_frame)
        self.scrollbar_tiles.grid(row=0, column=1, sticky=N + S)

        self.holder = Canvas(self.tiles_frame, bg="#34495e")
        self.holder.configure(highlightthickness=0, yscrollcommand=self.scrollbar_tiles.set)
        self.holder.grid(row=0, column=0, sticky=N + S + E + W)

        self.scrollbar_tiles.config(command=self.holder.yview)
        self.holder.configure(scrollregion=self.holder.bbox("all"))

        self.tiles_frame.grid_rowconfigure(0, weight=1)
        self.tiles_frame.grid_columnconfigure(0, weight=1)

        frame = Frame(self.holder, bg="#34495e", padx=30, pady=10)
        frame.rowconfigure(1, weight=1)
        frame.columnconfigure(1, weight=1)

        for t in self.tiles:
            print("generating tile...")
            tile_grid = t.get_tile_grid()
            tile_frame = Frame(frame, bg="#34495e")
            tile_frame.pack(fill=Y, padx=10, pady=10)

            rows = len(tile_grid)
            cols = len(max(tile_grid, key=len))

            tile_frame_array = [[" "] * cols for _ in range(rows)]

            for x in range(rows):
                for y in range(cols):
                    if isinstance(tile_grid[x][y], Cell):
                        label = Label(tile_frame, text=tile_grid[x][y].char, bd=1, width=2, bg=t.color)
                        label.grid(row=x, column=y)
                        tile_frame_array[x][y] = label

            t.frame = tile_frame_array
            self.tile_frames.append(tile_frame)

        self.holder.create_window(0, 0, anchor=NW, window=frame)

        frame.update_idletasks()

        self.holder.config(scrollregion=self.holder.bbox("all"))

        self.show_board()

    def show_board(self):

        self.board_frame = Frame(self.puz_frame, bg="gray", highlightthickness=0, pady=20)
        self.board_frame.pack()
        board = self.board_tile.get_tile_grid()

        rows = len(board)
        cols = len(max(board, key=len))

        board_frame_array = [[" "] * cols for _ in range(rows)]

        for x in range(rows):
            for y in range(cols):
                if isinstance(board[x][y], Cell):
                    label = Label(
                        self.board_frame, text=board[x][y].char, bd=1, font=("Helvetica", 20), relief=RAISED, width=2
                    )
                    label.grid(row=x, column=y)
                    board_frame_array[x][y] = label

        self.board_tile.frame = board_frame_array

        width = self.board_frame.winfo_reqwidth()
        self.board_frame.update()
        print(width)

        self.sols_frame = Frame(self.puz_frame, highlightthickness=0, height=1000, width=500)
        self.sols_frame.pack(fill=X)

        self.scrollbar_sols = AutoScrollbar(self.sols_frame)
        self.scrollbar_sols.grid(row=0, column=1, sticky=N + S)

        self.canvas_sol_holder = Canvas(self.sols_frame, bg="gray", height=1000)
        self.canvas_sol_holder.configure(highlightthickness=0, yscrollcommand=self.scrollbar_sols.set)
        self.canvas_sol_holder.grid(row=0, column=0, sticky=N + S + E + W)
        self.canvas_sol_holder.pack_propagate(FALSE)

        self.scrollbar_sols.config(command=self.canvas_sol_holder.yview)

        self.sols_frame.grid_rowconfigure(0, weight=1)
        self.sols_frame.grid_columnconfigure(0, weight=1)

        self.solutions_frame = Frame(self.canvas_sol_holder, bg="gray", height=1000)
        self.solutions_frame.rowconfigure(1, weight=1)
        self.solutions_frame.columnconfigure(1, weight=1)

        self.canvas_sol_holder.create_window(0, 0, anchor=NW, window=self.solutions_frame)
        self.solutions_frame.update_idletasks()
        self.canvas_sol_holder.config(scrollregion=self.canvas_sol_holder.bbox("all"))

    def clear(self):
        for t in self.tile_frames:
            t.destroy()
        if self.board_frame:
            self.board_frame.pack_forget()
            self.solutions_frame.pack_forget()
            self.sols_frame.grid_forget()
            self.canvas_sol_holder.destroy()
            self.scrollbar_sols.destroy()
        if self.holder:
            self.holder.destroy()
        if self.scrollbar_tiles:
            self.scrollbar_tiles.destroy()

    def parse_file(self):
        file_path = filedialog.askopenfilename(title="Open a file...")
        if file_path:
            self.clear()
            self.board_tile, self.tiles = parse_puzzle(file_path)
            self.filename.set("filename: " + path.basename(file_path))
            self.show_tiles()

    def solve_puzzle(self):
        if self.board_tile and self.tiles:
            solver = Solver(self.tiles, self.board_tile)
            solver.solutions_counter = self.solutions
            solver.solutions_frame = self.solutions_frame
            time = solver.solve_puzzle()

            print("time taken:", time)

            self.canvas_sol_holder.create_window(0, 0, anchor=NW, window=self.solutions_frame)
            self.solutions_frame.update_idletasks()
            self.canvas_sol_holder.config(scrollregion=self.canvas_sol_holder.bbox("all"))