NEW: visualization.Window(): simple Tk visualization class, can just draw some lines for now, but with working realignment, zooming and panning

This commit is contained in:
Stefan Harmuth 2023-12-28 06:09:16 +01:00
parent 8dea09c30f
commit 0aedd1c612
2 changed files with 59 additions and 1 deletions

View File

@ -1,6 +1,5 @@
bs4~=0.0.1 bs4~=0.0.1
beautifulsoup4~=4.12.2 beautifulsoup4~=4.12.2
fishhook~=0.2.9 fishhook~=0.2.9
pygame~=2.5.2
requests~=2.31.0 requests~=2.31.0
tqdm~=4.66 tqdm~=4.66

View File

@ -0,0 +1,59 @@
from __future__ import annotations
import tkinter as tk
from .coordinate import Line, Coordinate
class Window:
def __init__(self, width: int = 1280, height: int = 1024, bg_color: str = "black", fg_color: str = "white"):
self.width, self.height = width, height
self.bg_color, self.fg_color = bg_color, fg_color
self.__tk_root = tk.Tk()
self.__canvas = tk.Canvas(master=self.__tk_root, width=width, height=height, bg=bg_color)
self.__canvas.pack(fill=tk.BOTH, expand=tk.YES)
self.__canvas.bind("<MouseWheel>", self._zoom)
self.__canvas.bind("<ButtonPress-1>", self._scroll_start)
self.__canvas.bind("<B1-Motion>", self._scroll_move)
self.__boundary_box = [0, 0, 0, 0]
def _update_boundaries(self, coord: Coordinate | tuple[int, int]) -> None:
if coord[0] < self.__boundary_box[0]:
self.__boundary_box[0] = coord[0]
elif coord[0] > self.__boundary_box[2]:
self.__boundary_box[2] = coord[0]
if coord[1] < self.__boundary_box[1]:
self.__boundary_box[1] = coord[1]
elif coord[1] > self.__boundary_box[3]:
self.__boundary_box[3] = coord[1]
def _zoom(self, event: tk.Event) -> None:
amount = 0.95 if event.delta < 0 else 1.05
self.__canvas.scale(tk.ALL, 0, 0, amount, amount)
def _scroll_start(self, event: tk.Event) -> None:
self.__canvas.scan_mark(event.x, event.y)
def _scroll_move(self, event: tk.Event) -> None:
self.__canvas.scan_dragto(event.x, event.y, gain=1)
def clear(self) -> None:
self.__canvas.delete(tk.ALL)
def draw_line(self, line: Line) -> None:
self.__canvas.create_line(line.start.x, line.start.y, line.end.x, line.end.y, fill=self.fg_color, width=1)
self._update_boundaries(line.start)
self._update_boundaries(line.end)
def realign(self) -> None:
print(self.__boundary_box)
if self.__boundary_box[0] < 0:
self.__canvas.move(tk.ALL, abs(self.__boundary_box[0]) + 10, 0)
if self.__boundary_box[1] < 0:
self.__canvas.move(tk.ALL, 0, abs(self.__boundary_box[1]) + 10)
self.__canvas.update()
def done(self) -> None:
self.realign()
self.__canvas.mainloop()