|
| 1 | +# ---------------------------------------------------------------------------- |
| 2 | +# Python A-Turtle |
| 3 | +# ---------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Tiago Montes. |
| 5 | +# See LICENSE for details. |
| 6 | +# ---------------------------------------------------------------------------- |
| 7 | + |
| 8 | +import tkinter |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +class InvertedYTkCanvas(tkinter.Canvas): |
| 13 | + |
| 14 | + def __init__(self, master, background): |
| 15 | + |
| 16 | + super().__init__( |
| 17 | + master, |
| 18 | + highlightthickness=0, |
| 19 | + background=background, |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | + def _inverted_y(self, coords): |
| 24 | + |
| 25 | + return [ |
| 26 | + -value if offset % 2 else value |
| 27 | + for offset, value in enumerate(coords) |
| 28 | + ] |
| 29 | + |
| 30 | + |
| 31 | + def create_polygon(self, coords, *, fill, outline, width): |
| 32 | + |
| 33 | + return super().create_polygon( |
| 34 | + self._inverted_y(coords), |
| 35 | + fill=fill, |
| 36 | + outline=outline, |
| 37 | + width=width, |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | + def move(self, item_id, dx, dy): |
| 42 | + |
| 43 | + return super().move(item_id, dx, -dy) |
| 44 | + |
| 45 | + |
| 46 | + def create_line(self, coords, *, fill, width, capstyle): |
| 47 | + |
| 48 | + return super().create_line( |
| 49 | + self._inverted_y(coords), |
| 50 | + fill=fill, |
| 51 | + width=width, |
| 52 | + capstyle=capstyle, |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | + def coords(self, item_id, coords): |
| 57 | + |
| 58 | + return super().coords(item_id, self._inverted_y(coords)) |
| 59 | + |
0 commit comments