|
| 1 | +import tkinter as tk |
| 2 | +from typing import List, Tuple |
| 3 | + |
| 4 | +class ScaledCanvas: |
| 5 | + def __init__(self, root: tk.Tk, width_m: float, height_m: float): |
| 6 | + self.canvas = tk.Canvas(root, bg="white") |
| 7 | + self.canvas.pack(fill=tk.BOTH, expand=True) |
| 8 | + self.width_m = width_m |
| 9 | + self.height_m = height_m |
| 10 | + self.scale = 1 |
| 11 | + self.offset_x = 0 |
| 12 | + self.offset_y = 0 |
| 13 | + self.objects = [] |
| 14 | + |
| 15 | + # Recalculate scale on resize |
| 16 | + self.canvas.bind("<Configure>", self._on_resize) |
| 17 | + |
| 18 | + def _on_resize(self, event): |
| 19 | + # Calculate scale to maintain aspect ratio |
| 20 | + scale_width = event.width / self.width_m |
| 21 | + scale_height = event.height / self.height_m |
| 22 | + self.scale = min(scale_width, scale_height) |
| 23 | + |
| 24 | + # Calculate offsets to center the drawing |
| 25 | + self.offset_x = (event.width - self.scale * self.width_m) / 2 |
| 26 | + self.offset_y = (event.height - self.scale * self.height_m) / 2 |
| 27 | + |
| 28 | + self.canvas.delete("all") |
| 29 | + for obj in self.objects: |
| 30 | + obj() |
| 31 | + |
| 32 | + def add_rectangle(self, x_m: float, y_m: float, width_m: float, height_m: float, color: str = "black"): |
| 33 | + def draw(): |
| 34 | + x1, y1 = self._to_pixels(x_m, y_m) |
| 35 | + x2, y2 = self._to_pixels(x_m + width_m, y_m + height_m) |
| 36 | + self.canvas.create_rectangle(x1, y1, x2, y2, outline=color, width=2) |
| 37 | + |
| 38 | + self.objects.append(draw) |
| 39 | + draw() |
| 40 | + |
| 41 | + def add_arrow(self, start_m: Tuple[float, float], end_m: Tuple[float, float], color: str = "red"): |
| 42 | + def draw(): |
| 43 | + x1, y1 = self._to_pixels(*start_m) |
| 44 | + x2, y2 = self._to_pixels(*end_m) |
| 45 | + self.canvas.create_line(x1, y1, x2, y2, arrow=tk.LAST, fill=color, width=2) |
| 46 | + |
| 47 | + self.objects.append(draw) |
| 48 | + draw() |
| 49 | + |
| 50 | + def _to_pixels(self, x_m: float, y_m: float) -> Tuple[int, int]: |
| 51 | + return ( |
| 52 | + int(x_m * self.scale + self.offset_x), |
| 53 | + int(y_m * self.scale + self.offset_y), |
| 54 | + ) |
| 55 | + |
| 56 | +# Create the main Tkinter application |
| 57 | +def main(): |
| 58 | + root = tk.Tk() |
| 59 | + root.title("Scaled Canvas Example with Fixed Aspect Ratio") |
| 60 | + width_m, height_m = 10.0, 5.0 # Visualization dimensions in meters |
| 61 | + canvas = ScaledCanvas(root, width_m, height_m) |
| 62 | + |
| 63 | + # Add a rectangle |
| 64 | + canvas.add_rectangle(1.0, 1.0, 8.0, 3.0, color="blue") |
| 65 | + |
| 66 | + # Add some arrows |
| 67 | + arrows = [ |
| 68 | + ((1.0, 1.0), (2.0, 2.0)), |
| 69 | + ((3.0, 1.0), (4.0, 2.5)), |
| 70 | + ((8.0, 1.0), (7.0, 3.0)), |
| 71 | + ((5.0, 4.0), (5.0, 2.5)) |
| 72 | + ] |
| 73 | + for start, end in arrows: |
| 74 | + canvas.add_arrow(start, end, color="green") |
| 75 | + |
| 76 | + root.mainloop() |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main() |
0 commit comments