Skip to content

Commit 4dae75f

Browse files
committed
Minimal Canvas with larger Y values on the top of the screen.
1 parent 28e8f06 commit 4dae75f

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

src/aturtle/canvas.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+

src/aturtle/window.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import tkinter
99

10+
from . import canvas
11+
1012

1113

1214
class Window:
@@ -18,7 +20,8 @@ class Window:
1820
_windows = []
1921

2022
def __init__(self, width=320, height=320, x=None, y=None,
21-
fill_color='white', title='A-Turtle'):
23+
fill_color='white', title='A-Turtle',
24+
canvas_factory=canvas.InvertedYTkCanvas):
2225
"""
2326
Initialize a Window with the given `width` and `height`, filled in
2427
`fill_color`, with the given `title`.
@@ -47,9 +50,8 @@ def __init__(self, width=320, height=320, x=None, y=None,
4750
y = (tk_window.winfo_screenheight() - height) // 2 if y is None else y
4851

4952
tk_window.geometry(f'{width}x{height}+{x}+{y}')
50-
canvas = tkinter.Canvas(
53+
canvas = canvas_factory(
5154
tk_window,
52-
highlightthickness=0,
5355
background=fill_color,
5456
)
5557
canvas.pack(expand=True, fill='both')

0 commit comments

Comments
 (0)