commit 72dad848ec306cfda53d603607583bdb95e26207 Author: Dan Anglin Date: Mon Feb 12 19:53:45 2024 +0000 feat: run a simple graphical window Run a simple window and draw a few lines on it. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..225fc6f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/__pycache__ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5200de6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Dan Anglin + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the “Software”), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/graphics.py b/graphics.py new file mode 100644 index 0000000..5e8f868 --- /dev/null +++ b/graphics.py @@ -0,0 +1,80 @@ +from tkinter import Tk, BOTH, Canvas + +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. + """ + + def __init__(self, point_a: Point, point_b: Point): + self.point_a = point_a + self.point_b = point_b + + def draw(self, canvas: Canvas, fill_colour: str): + """ + 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 + ) + canvas.pack() + + +class Window: + """ + Window is a Graphical window. + """ + + def __init__(self, width: int, height: int): + self.width = width + self.height = height + + self.__root = Tk() + self.__root.title("Maze Solver") + self.__root.geometry(f"{self.width}x{self.height}") + + self.canvas = Canvas() + self.canvas.pack() + + self.is_running = False + + self.__root.protocol("WM_DELETE_WINDOW", self.close) + + def redraw(self): + """ + redraw redraws all the graphics in the window. + """ + self.__root.update_idletasks() + self.__root.update() + + def wait_for_close(self): + """ + wait_for_close continuously redraws the window until + it is set to close. + """ + self.is_running = True + while self.is_running: + self.redraw() + + def draw_line(self, line: Line, fill_colour: str): + """ + draw_line draws a line on the canvas. + """ + line.draw(self.canvas, fill_colour) + + def close(self): + """ + close sets the window to close. + """ + self.is_running = False diff --git a/main.py b/main.py new file mode 100644 index 0000000..d1fd73e --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +from graphics import Window, Line, Point + + +def main(): + window = Window(800, 800) + line_1 = Line(Point(10, 50), Point(100, 70)) + line_2 = Line(Point(300, 145), Point(456, 200)) + line_3 = Line(Point(20, 498), Point(50, 600)) + window.draw_line(line_1, "black") + window.draw_line(line_2, "red") + window.draw_line(line_3, "black") + window.wait_for_close() + + +if __name__ == "__main__": + main()