The problem is that I made a short algorithm that finds it's way through a maze and marks all squares that it has visited in blue. The core program works fine but the problem is that the GUI only displays the maze with it's visited squares after the whole process is done. This would not normally be a problem but I need to be able to visibly see the algorithm traverse the maze as it goes. The problem is that when I call the UpdateMaze
function every iteration of the searching algorithm, it doesn't appear to take affect until the whole traversal is finished.
- Wall images are just a black square GIF
- Space images are just a white square GIF
- Edge images are just a Red square GIF
- Finish is a Green square GIF
- Visited is a blue square GIF
import tkinter as tk
from tkinter import *
import time
class MazeGUI():
def __init__(self):
self.maze =[
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[4, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 4],
[4, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 4],
[4, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 4],
[4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 4],
[4, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 4],
[4, 0, 1, 2, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 4],
[4, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 4],
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
]
self.wall = tk.PhotoImage(file = "MazePiece_Wall.gif")
self.space = tk.PhotoImage(file = "MazePiece_Space.gif")
self.edge = tk.PhotoImage(file = "MazePiece_Outer.gif")
self.visited = tk.PhotoImage(file = "MazePiece_Visited.gif")
self.finish = tk.PhotoImage(file = "MazePiece_Finish.gif")
def UpdateMaze(self):
for y in range(len(self.maze)):
for x in range(len(self.maze[y])):
if self.maze[y][x] == 0:
label = Label(root, image=self.space,
width=20, height=20).grid(row=y, column=x)
elif self.maze[y][x] == 1:
label = Label(root, image=self.wall,
width=20, height=20).grid(row=y, column=x)
elif self.maze[y][x] == 2:
label = Label(root, image=self.finish,
width=20, height=20).grid(row=y, column=x)
elif self.maze[y][x] == 3:
label = Label(root, image=self.visited,
width=20, height=20).grid(row=y, column=x)
elif self.maze[y][x] == 4:
label = Label(root, image=self.edge,
width=20, height=20).grid(row=y, column=x)
def Move(Maze,x,y):
if Maze.maze[y][x] == 2:
return True
elif Maze.maze[y][x] == 1:
return False
elif Maze.maze[y][x] == 3:
return False
elif Maze.maze[y][x] == 4:
return False
Maze.maze[y][x] = 3
if ((x < len(Maze.maze)-1 and Move(Maze,x+1, y))
or (y > 0 and Move(Maze,x, y-1))
or (x > 0 and Move(Maze,x-1, y))
or (y < len(Maze.maze)-1 and Move(Maze,x, y+1))):
return True
return False
root = Tk()
Maze = MazeGUI()
root.lift()
StartPosX = 1
StartPosY = 1
Move(Maze,StartPosX,StartPosY)
Maze.UpdateMaze()
root.mainloop()