2024-02-12 19:53:45 +00:00
|
|
|
from tkinter import Tk, BOTH, Canvas
|
|
|
|
|
2024-02-12 20:04:55 +00:00
|
|
|
|
2024-02-12 19:53:45 +00:00
|
|
|
class Point:
|
|
|
|
"""
|
|
|
|
Point represents the position of a point.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, x: int, y: int):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
|
|
|
|
|
|
|
class Line:
|
|
|
|
"""
|
|
|
|
Line represents a graphical line.
|
|
|
|
"""
|
|
|
|
|
2024-02-13 10:59:47 +00:00
|
|
|
def __init__(self, point_a: Point, point_b: Point) -> None:
|
2024-02-12 19:53:45 +00:00
|
|
|
self.point_a = point_a
|
|
|
|
self.point_b = point_b
|
|
|
|
|
2024-02-13 10:59:47 +00:00
|
|
|
def draw(self, canvas: Canvas, fill_colour: str) -> None:
|
2024-02-12 19:53:45 +00:00
|
|
|
"""
|
|
|
|
draw draws a line on a given canvas.
|
|
|
|
"""
|
|
|
|
canvas.create_line(
|
|
|
|
self.point_a.x, self.point_a.y,
|
|
|
|
self.point_b.x, self.point_b.y,
|
|
|
|
fill=fill_colour, width=2
|
|
|
|
)
|
2024-02-14 22:50:46 +00:00
|
|
|
canvas.pack(fill=BOTH, expand=1)
|
2024-02-12 19:53:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Window:
|
|
|
|
"""
|
|
|
|
Window is a Graphical window.
|
|
|
|
"""
|
|
|
|
|
2024-02-13 10:59:47 +00:00
|
|
|
def __init__(self, width: int, height: int) -> None:
|
2024-02-13 13:39:25 +00:00
|
|
|
self._root = Tk()
|
|
|
|
self._root.title("Maze Solver")
|
2024-02-12 19:53:45 +00:00
|
|
|
|
2024-02-12 21:30:27 +00:00
|
|
|
# Position the window to the centre of the screen
|
2024-02-13 13:39:25 +00:00
|
|
|
screen_width = self._root.winfo_screenwidth()
|
|
|
|
screen_height = self._root.winfo_screenheight()
|
2024-02-12 21:30:27 +00:00
|
|
|
centre_x = int(screen_width/2 - width/2)
|
|
|
|
centre_y = int(screen_height/2 - height/2)
|
2024-02-13 13:39:25 +00:00
|
|
|
self._root.geometry(f"{width}x{height}+{centre_x}+{centre_y}")
|
2024-02-12 21:30:27 +00:00
|
|
|
|
2024-02-13 15:52:28 +00:00
|
|
|
# Styling
|
|
|
|
self.background_colour = "white"
|
|
|
|
self.cell_grid_colour = "black"
|
|
|
|
|
2024-02-13 13:39:25 +00:00
|
|
|
self._canvas = Canvas(self._root)
|
|
|
|
self._canvas.config(
|
2024-02-13 15:52:28 +00:00
|
|
|
bg=self.background_colour,
|
2024-02-12 20:04:55 +00:00
|
|
|
height=height,
|
|
|
|
width=width,
|
|
|
|
)
|
2024-02-13 13:39:25 +00:00
|
|
|
self._canvas.pack()
|
2024-02-12 19:53:45 +00:00
|
|
|
|
2024-02-13 10:59:47 +00:00
|
|
|
def redraw(self) -> None:
|
2024-02-12 19:53:45 +00:00
|
|
|
"""
|
|
|
|
redraw redraws all the graphics in the window.
|
|
|
|
"""
|
2024-02-13 13:39:25 +00:00
|
|
|
self._root.update_idletasks()
|
|
|
|
self._root.update()
|
2024-02-12 19:53:45 +00:00
|
|
|
|
2024-02-15 01:40:16 +00:00
|
|
|
def mainloop(self) -> None:
|
2024-02-12 19:53:45 +00:00
|
|
|
"""
|
2024-02-15 01:40:16 +00:00
|
|
|
mainloop calls the root widget's mainloop method to
|
|
|
|
ensure that the window remains visible on the screen.
|
2024-02-12 19:53:45 +00:00
|
|
|
"""
|
2024-02-15 01:40:16 +00:00
|
|
|
self._root.mainloop()
|
2024-02-12 19:53:45 +00:00
|
|
|
|
2024-02-13 10:59:47 +00:00
|
|
|
def draw_line(self, line: Line, fill_colour: str = "black") -> None:
|
2024-02-12 19:53:45 +00:00
|
|
|
"""
|
|
|
|
draw_line draws a line on the canvas.
|
|
|
|
"""
|
2024-02-13 13:39:25 +00:00
|
|
|
line.draw(self._canvas, fill_colour)
|