def perform_process(self, function, message): # Popup initialization popup = Tkinter.Toplevel(self) popup.grab_set() popup.wm_title("Working...") if sys.platform.startswith('linux'): img = Tkinter.PhotoImage(file = filepath.get_filepath("assets", "icon.png")) popup.tk.call('wm', 'iconphoto', popup._w, img) else: popup.iconbitmap(os.path.abspath(filepath.get_filepath("assets", "icon.ico"))) popup.geometry( '%dx%d+%d+%d' % (250, 50, self.winfo_x() + 50, self.winfo_y() + 100) ) popup.resizable(False, False) info_label = Tkinter.Label(popup, text = message) info_label.place(relx = 0.5, rely = 0.5, anchor = Tkinter.CENTER) # Starting process thread = threading.Thread(target = function) thread.start() while thread.is_alive(): popup.update() time.sleep(0.001) popup.grab_release() popup.destroy()
def print_output(self, result, filename): # String -> List pixel_list = [] for c in result: if c == '0': pixel_list.append((255, 255, 255)) else: pixel_list.append(0) # Creating starting point for i in range(1, self.w): if pixel_list[i] == 0 and pixel_list[i + self.w + 1] == (255, 255, 255): pixel_list[i] = (255, 255, 255) break # Creating exit point for i in range(len(pixel_list) - 2, len(pixel_list) - self.w, -1): if pixel_list[i] == 0 and pixel_list[i - self.w - 1] == (255, 255, 255): pixel_list[i] = (255, 255, 255) break # Writing image to file img = Image.new("RGB", (self.w + 1, self.h + 1)) img.putdata(pixel_list) img.save(filepath.get_filepath("mazes", filename))
def __init__(self, filename): self.pixel_map = [] self.h = 0 self.w = 0 # Opening image file im = Image.open(filepath.get_filepath("mazes", filename)) im = im.convert("RGB") # Getting values from the image object pixel_list = list(im.getdata()) # Getting image size self.w, self.h = im.size # Getting image mode self.mode = im.mode # Creating a one-filled pixel map (1 for white (hall), 0 for non-white (wall)) self.pixel_map = [[1 for i in range(self.w)] for j in range(self.h)] # Determing the number of threads (hardware dependant) try: thread_num = psutil.cpu_count() except: thread_num = 2 threads = [] bottom = 0 top = self.h / thread_num index = 0 # Using threads to convert pixel_list into the pixel_map # print "Creating threads..." for i in range(thread_num): threads.append( threading.Thread(target=self.fill_out_pixel_map, args=( pixel_list, bottom, top, index, ))) bottom = top top = (i + 2) * self.h / thread_num index = bottom * self.w # print "Starting threads..." for t in threads: t.start() # Waiting for the threads to finish # Program will continue to execute when both threads are finished for t in threads: t.join()
import Tkinter import os, sys import gui import filepath if __name__ == "__main__": app = gui.Application(None) app.title("Maze Pathfinder") # Window icon setup if sys.platform.startswith('linux'): img = Tkinter.PhotoImage(file = filepath.get_filepath("assets", "icon.png")) app.tk.call('wm', 'iconphoto', app._w, img) else: app.iconbitmap(os.path.abspath(filepath.get_filepath("assets", "icon.ico"))) # Starting main program loop app.mainloop()
def write(self, filename): new_filename = filepath.get_filepath("mazes", filename) self.img.save(new_filename) return new_filename
def initialize(self): # Grid which will contain our widgets self.grid() # Our window will not be resizable by default self.resizable(False, False) # Main menu menu_MainMenu = Tkinter.Menu(self) menu_MainMenu.add_command(label = "Help", command = self.show_help) menu_MainMenu.add_command(label = "About", command = self.show_about) menu_MainMenu.add_command(label = "Exit", command = sys.exit) self.config(menu = menu_MainMenu) # Main group self.grp_solver = Tkinter.LabelFrame(self, text = "Maze solver", padx = 5, pady = 5) self.grp_solver.grid(column = 0, row = 0, padx = 10, pady = 5, columnspan = 5) # Filename label lbl_filename = Tkinter.Label(self.grp_solver, anchor = "w", text = "Input:" ) lbl_filename.grid( column = 0, row = 0, sticky = "ew", padx = 10, pady = 10 ) # Entry for filename self.ent_filename = Tkinter.Entry(self.grp_solver) self.ent_filename.grid( column = 1, row = 0, sticky = "ew", padx = 10, pady = 10 ) self.ent_filename.insert(-1, "normal.bmp") self.ent_filename.bind("<Return>", self.ent_filename_on_enter) # Button to load image from file btn_import = Tkinter.Button(self.grp_solver, text = "Import", command = self.btn_import_on_click ) btn_import.grid( column = 2, row = 0, padx = 5, pady = 5 ) # Browse button btn_import = Tkinter.Button(self.grp_solver, text = "Browse", command = self.open_dialog ) btn_import.grid( column = 3, row = 0, padx = 5, pady = 5 ) # Group for traverse method radio buttons grp_method = Tkinter.LabelFrame(self.grp_solver, text = "Traverse method:", padx = 5, pady = 5) grp_method.grid(column = 0, row = 1, padx = 10, pady = 5, columnspan = 4) def process_dfs_select(): self.dfs_query = tkMessageBox.askquestion("Info", "Use iterative (yes) or recursive (no)?") self.disable_heuristic() # Radio buttons for the traverse method self.rbSelectedValue = Tkinter.StringVar() rb_DFS = Tkinter.Radiobutton(grp_method, text = "DFS", variable = self.rbSelectedValue, value = "DFS", command = process_dfs_select, ) rb_BFS = Tkinter.Radiobutton(grp_method, text = "BFS", variable = self.rbSelectedValue, value = "BFS", command = self.disable_heuristic ) rb_Dijkstra = Tkinter.Radiobutton(grp_method, text = "Dijkstra", variable = self.rbSelectedValue, value = "Dijkstra", command = self.disable_heuristic ) rb_Astar = Tkinter.Radiobutton(grp_method, text = "A*", variable = self.rbSelectedValue, value = "Astar", command = self.activate_heuristic ) rb_DFS.grid(column = 1, row = 1, padx = 5) rb_BFS.grid(column = 2, row = 1, padx = 5) rb_Dijkstra.grid(column = 3, row = 1, padx = 5) rb_Astar.grid(column = 4, row = 1, padx = 5) rb_BFS.select() # Group for heuristic grp_heuristic = Tkinter.LabelFrame(grp_method, text = "Heuristic (for A*):", padx = 5, pady = 5) grp_heuristic.grid(column = 1, row = 2, padx = 10, pady = 5, columnspan = 4) self.rb_heuristic_value = Tkinter.IntVar() self.rb_heur_manhattan = Tkinter.Radiobutton(grp_heuristic, text = "Manhattan", variable = self.rb_heuristic_value, value = 0 ) self.rb_heur_euclidean = Tkinter.Radiobutton(grp_heuristic, text = "Euclidean", variable = self.rb_heuristic_value, value = 1 ) self.rb_heur_manhattan.grid(column = 0, row = 0, padx = 5) self.rb_heur_euclidean.grid(column = 1, row = 0, padx = 5) self.disable_heuristic() # Button to solve the maze btn_solve = Tkinter.Button(self.grp_solver, text = "Solve maze!", width = 30, command = self.btn_solve_on_click ) btn_solve.grid(column = 1, row = 2, columnspan = 2, pady = 5) # Color choosers self.btn_color_from = Tkinter.Button(self.grp_solver, text = " ", command = self.choose_color_from, bg = "#00ff00" ) self.btn_color_to = Tkinter.Button(self.grp_solver, text = " ", command = self.choose_color_to, bg = "#0000ff" ) self.btn_color_from.grid(column = 0, row = 2) self.btn_color_to.grid(column = 3, row = 2) # Open solution checkbox self.show_solution = Tkinter.BooleanVar() cb_show_solution = Tkinter.Checkbutton(self.grp_solver, text = "Open solution when finished", variable = self.show_solution ) cb_show_solution.grid( column = 0, row = 3, columnspan = 5, padx = 20, pady = 5 ) cb_show_solution.select() # Expand button self.img_expand = Tkinter.PhotoImage(file=filepath.get_filepath("assets", "expand.gif")) self.img_shrink = Tkinter.PhotoImage(file=filepath.get_filepath("assets", "shrink.gif")) self.btn_expand = Tkinter.Button(self, width = 50, height = 1, command = self.toggle_expand ) self.btn_expand.grid( column = 0, row = 5, columnspan = 5, padx = 0, pady = 0 ) self.btn_expand.config(image=self.img_expand, height = 20, width = 355)