1 from tkinter import Canvas, PhotoImage
2 from typing import Any, Callable, Dict, Sequence, Tuple, TypeVar, Union, overload
4 # Note: '_Color' is the alias we use for arguments and _AnyColor is the
5 # alias we use for return types. Really, these two aliases should be the
6 # same, but as per the "no union returns" typeshed policy, we'll return
8 _Color = Union[str, Tuple[float, float, float]]
11 # TODO: Replace this with a TypedDict once it becomes standardized.
12 _PenState = Dict[str, Any]
14 _Speed = Union[str, float]
15 _PolygonCoords = Sequence[Tuple[float, float]]
17 # TODO: Type this more accurately
18 # Vec2D is actually a custom subclass of 'tuple'.
19 Vec2D = Tuple[float, float]
21 class TurtleScreenBase(object):
27 def __init__(self, cv: Canvas) -> None: ...
28 def mainloop(self) -> None: ...
29 def textinput(self, title: str, prompt: str) -> str | None: ...
31 self, title: str, prompt: str, default: float | None = ..., minval: float | None = ..., maxval: float | None = ...
32 ) -> float | None: ...
34 class Terminator(Exception): ...
35 class TurtleGraphicsError(Exception): ...
38 def __init__(self, type_: str, data: _PolygonCoords | PhotoImage | None = ...) -> None: ...
39 def addcomponent(self, poly: _PolygonCoords, fill: _Color, outline: _Color | None = ...) -> None: ...
41 class TurtleScreen(TurtleScreenBase):
42 def __init__(self, cv: Canvas, mode: str = ..., colormode: float = ..., delay: int = ...) -> None: ...
43 def clear(self) -> None: ...
45 def mode(self, mode: None = ...) -> str: ...
47 def mode(self, mode: str) -> None: ...
48 def setworldcoordinates(self, llx: float, lly: float, urx: float, ury: float) -> None: ...
49 def register_shape(self, name: str, shape: _PolygonCoords | Shape | None = ...) -> None: ...
51 def colormode(self, cmode: None = ...) -> float: ...
53 def colormode(self, cmode: float) -> None: ...
54 def reset(self) -> None: ...
55 def turtles(self) -> list[Turtle]: ...
57 def bgcolor(self) -> _AnyColor: ...
59 def bgcolor(self, color: _Color) -> None: ...
61 def bgcolor(self, r: float, g: float, b: float) -> None: ...
63 def tracer(self, n: None = ...) -> int: ...
65 def tracer(self, n: int, delay: int | None = ...) -> None: ...
67 def delay(self, delay: None = ...) -> int: ...
69 def delay(self, delay: int) -> None: ...
70 def update(self) -> None: ...
71 def window_width(self) -> int: ...
72 def window_height(self) -> int: ...
73 def getcanvas(self) -> Canvas: ...
74 def getshapes(self) -> list[str]: ...
75 def onclick(self, fun: Callable[[float, float], Any], btn: int = ..., add: Any | None = ...) -> None: ...
76 def onkey(self, fun: Callable[[], Any], key: str) -> None: ...
77 def listen(self, xdummy: float | None = ..., ydummy: float | None = ...) -> None: ...
78 def ontimer(self, fun: Callable[[], Any], t: int = ...) -> None: ...
80 def bgpic(self, picname: None = ...) -> str: ...
82 def bgpic(self, picname: str) -> None: ...
84 def screensize(self, canvwidth: None = ..., canvheight: None = ..., bg: None = ...) -> tuple[int, int]: ...
85 # Looks like if self.cv is not a ScrolledCanvas, this could return a tuple as well
87 def screensize(self, canvwidth: int, canvheight: int, bg: _Color | None = ...) -> None: ...
88 onscreenclick = onclick
91 addshape = register_shape
92 def onkeypress(self, fun: Callable[[], Any], key: str | None = ...) -> None: ...
95 class TNavigator(object):
96 START_ORIENTATION: dict[str, Vec2D]
98 DEFAULT_ANGLEOFFSET: int
99 DEFAULT_ANGLEORIENT: int
100 def __init__(self, mode: str = ...) -> None: ...
101 def reset(self) -> None: ...
102 def degrees(self, fullcircle: float = ...) -> None: ...
103 def radians(self) -> None: ...
104 def forward(self, distance: float) -> None: ...
105 def back(self, distance: float) -> None: ...
106 def right(self, angle: float) -> None: ...
107 def left(self, angle: float) -> None: ...
108 def pos(self) -> Vec2D: ...
109 def xcor(self) -> float: ...
110 def ycor(self) -> float: ...
112 def goto(self, x: tuple[float, float], y: None = ...) -> None: ...
114 def goto(self, x: float, y: float) -> None: ...
115 def home(self) -> None: ...
116 def setx(self, x: float) -> None: ...
117 def sety(self, y: float) -> None: ...
119 def distance(self, x: TNavigator | tuple[float, float], y: None = ...) -> float: ...
121 def distance(self, x: float, y: float) -> float: ...
123 def towards(self, x: TNavigator | tuple[float, float], y: None = ...) -> float: ...
125 def towards(self, x: float, y: float) -> float: ...
126 def heading(self) -> float: ...
127 def setheading(self, to_angle: float) -> None: ...
128 def circle(self, radius: float, extent: float | None = ..., steps: int | None = ...) -> None: ...
140 def __init__(self, resizemode: str = ...) -> None: ...
142 def resizemode(self, rmode: None = ...) -> str: ...
144 def resizemode(self, rmode: str) -> None: ...
146 def pensize(self, width: None = ...) -> int: ...
148 def pensize(self, width: int) -> None: ...
149 def penup(self) -> None: ...
150 def pendown(self) -> None: ...
151 def isdown(self) -> bool: ...
153 def speed(self, speed: None = ...) -> int: ...
155 def speed(self, speed: _Speed) -> None: ...
157 def pencolor(self) -> _AnyColor: ...
159 def pencolor(self, color: _Color) -> None: ...
161 def pencolor(self, r: float, g: float, b: float) -> None: ...
163 def fillcolor(self) -> _AnyColor: ...
165 def fillcolor(self, color: _Color) -> None: ...
167 def fillcolor(self, r: float, g: float, b: float) -> None: ...
169 def color(self) -> tuple[_AnyColor, _AnyColor]: ...
171 def color(self, color: _Color) -> None: ...
173 def color(self, r: float, g: float, b: float) -> None: ...
175 def color(self, color1: _Color, color2: _Color) -> None: ...
176 def showturtle(self) -> None: ...
177 def hideturtle(self) -> None: ...
178 def isvisible(self) -> bool: ...
179 # Note: signatures 1 and 2 overlap unsafely when no arguments are provided
181 def pen(self) -> _PenState: ... # type: ignore
185 pen: _PenState | None = ...,
189 pencolor: _Color = ...,
190 fillcolor: _Color = ...,
193 resizemode: str = ...,
194 stretchfactor: tuple[float, float] = ...,
208 class RawTurtle(TPen, TNavigator):
210 self, canvas: Canvas | TurtleScreen | None = ..., shape: str = ..., undobuffersize: int = ..., visible: bool = ...
212 def reset(self) -> None: ...
213 def setundobuffer(self, size: int | None) -> None: ...
214 def undobufferentries(self) -> int: ...
215 def clear(self) -> None: ...
216 def clone(self: _T) -> _T: ...
218 def shape(self, name: None = ...) -> str: ...
220 def shape(self, name: str) -> None: ...
221 # Unsafely overlaps when no arguments are provided
223 def shapesize(self) -> tuple[float, float, float]: ... # type: ignore
226 self, stretch_wid: float | None = ..., stretch_len: float | None = ..., outline: float | None = ...
229 def shearfactor(self, shear: None = ...) -> float: ...
231 def shearfactor(self, shear: float) -> None: ...
232 # Unsafely overlaps when no arguments are provided
234 def shapetransform(self) -> tuple[float, float, float, float]: ... # type: ignore
237 self, t11: float | None = ..., t12: float | None = ..., t21: float | None = ..., t22: float | None = ...
239 def get_shapepoly(self) -> _PolygonCoords | None: ...
240 def settiltangle(self, angle: float) -> None: ...
242 def tiltangle(self, angle: None = ...) -> float: ...
244 def tiltangle(self, angle: float) -> None: ...
245 def tilt(self, angle: float) -> None: ...
246 # Can return either 'int' or Tuple[int, ...] based on if the stamp is
247 # a compound stamp or not. So, as per the "no Union return" policy,
249 def stamp(self) -> Any: ...
250 def clearstamp(self, stampid: int | Tuple[int, ...]) -> None: ...
251 def clearstamps(self, n: int | None = ...) -> None: ...
252 def filling(self) -> bool: ...
253 def begin_fill(self) -> None: ...
254 def end_fill(self) -> None: ...
255 def dot(self, size: int | None = ..., *color: _Color) -> None: ...
256 def write(self, arg: object, move: bool = ..., align: str = ..., font: tuple[str, int, str] = ...) -> None: ...
257 def begin_poly(self) -> None: ...
258 def end_poly(self) -> None: ...
259 def get_poly(self) -> _PolygonCoords | None: ...
260 def getscreen(self) -> TurtleScreen: ...
261 def getturtle(self: _T) -> _T: ...
263 def onclick(self, fun: Callable[[float, float], Any], btn: int = ..., add: bool | None = ...) -> None: ...
264 def onrelease(self, fun: Callable[[float, float], Any], btn: int = ..., add: bool | None = ...) -> None: ...
265 def ondrag(self, fun: Callable[[float, float], Any], btn: int = ..., add: bool | None = ...) -> None: ...
266 def undo(self) -> None: ...
267 turtlesize = shapesize
269 class _Screen(TurtleScreen):
270 def __init__(self) -> None: ...
271 # Note int and float are interpreted differently, hence the Union instead of just float
273 self, width: int | float = ..., height: int | float = ..., startx: int | None = ..., starty: int | None = ...
275 def title(self, titlestring: str) -> None: ...
276 def bye(self) -> None: ...
277 def exitonclick(self) -> None: ...
279 class Turtle(RawTurtle):
280 def __init__(self, shape: str = ..., undobuffersize: int = ..., visible: bool = ...) -> None: ...
285 def write_docstringdict(filename: str = ...) -> None: ...
287 # Note: it's somewhat unfortunate that we have to copy the function signatures.
288 # It would be nice if we could partially reduce the redundancy by doing something
289 # like the following:
292 # clear = _screen.clear
294 # However, it seems pytype does not support this type of syntax in pyi files.
296 # Functions copied from TurtleScreenBase:
298 # Note: mainloop() was always present in the global scope, but was added to
299 # TurtleScreenBase in Python 3.0
300 def mainloop() -> None: ...
301 def textinput(title: str, prompt: str) -> str | None: ...
303 title: str, prompt: str, default: float | None = ..., minval: float | None = ..., maxval: float | None = ...
304 ) -> float | None: ...
306 # Functions copied from TurtleScreen:
308 def clear() -> None: ...
310 def mode(mode: None = ...) -> str: ...
312 def mode(mode: str) -> None: ...
313 def setworldcoordinates(llx: float, lly: float, urx: float, ury: float) -> None: ...
314 def register_shape(name: str, shape: _PolygonCoords | Shape | None = ...) -> None: ...
316 def colormode(cmode: None = ...) -> float: ...
318 def colormode(cmode: float) -> None: ...
319 def reset() -> None: ...
320 def turtles() -> list[Turtle]: ...
322 def bgcolor() -> _AnyColor: ...
324 def bgcolor(color: _Color) -> None: ...
326 def bgcolor(r: float, g: float, b: float) -> None: ...
328 def tracer(n: None = ...) -> int: ...
330 def tracer(n: int, delay: int | None = ...) -> None: ...
332 def delay(delay: None = ...) -> int: ...
334 def delay(delay: int) -> None: ...
335 def update() -> None: ...
336 def window_width() -> int: ...
337 def window_height() -> int: ...
338 def getcanvas() -> Canvas: ...
339 def getshapes() -> list[str]: ...
340 def onclick(fun: Callable[[float, float], Any], btn: int = ..., add: Any | None = ...) -> None: ...
341 def onkey(fun: Callable[[], Any], key: str) -> None: ...
342 def listen(xdummy: float | None = ..., ydummy: float | None = ...) -> None: ...
343 def ontimer(fun: Callable[[], Any], t: int = ...) -> None: ...
345 def bgpic(picname: None = ...) -> str: ...
347 def bgpic(picname: str) -> None: ...
349 def screensize(canvwidth: None = ..., canvheight: None = ..., bg: None = ...) -> tuple[int, int]: ...
351 def screensize(canvwidth: int, canvheight: int, bg: _Color | None = ...) -> None: ...
353 onscreenclick = onclick
356 addshape = register_shape
358 def onkeypress(fun: Callable[[], Any], key: str | None = ...) -> None: ...
362 # Functions copied from _Screen:
364 def setup(width: float = ..., height: float = ..., startx: int | None = ..., starty: int | None = ...) -> None: ...
365 def title(titlestring: str) -> None: ...
366 def bye() -> None: ...
367 def exitonclick() -> None: ...
368 def Screen() -> _Screen: ...
370 # Functions copied from TNavigator:
372 def degrees(fullcircle: float = ...) -> None: ...
373 def radians() -> None: ...
374 def forward(distance: float) -> None: ...
375 def back(distance: float) -> None: ...
376 def right(angle: float) -> None: ...
377 def left(angle: float) -> None: ...
378 def pos() -> Vec2D: ...
379 def xcor() -> float: ...
380 def ycor() -> float: ...
382 def goto(x: tuple[float, float], y: None = ...) -> None: ...
384 def goto(x: float, y: float) -> None: ...
385 def home() -> None: ...
386 def setx(x: float) -> None: ...
387 def sety(y: float) -> None: ...
389 def distance(x: TNavigator | tuple[float, float], y: None = ...) -> float: ...
391 def distance(x: float, y: float) -> float: ...
393 def towards(x: TNavigator | tuple[float, float], y: None = ...) -> float: ...
395 def towards(x: float, y: float) -> float: ...
396 def heading() -> float: ...
397 def setheading(to_angle: float) -> None: ...
398 def circle(radius: float, extent: float | None = ..., steps: int | None = ...) -> None: ...
410 # Functions copied from TPen:
412 def resizemode(rmode: None = ...) -> str: ...
414 def resizemode(rmode: str) -> None: ...
416 def pensize(width: None = ...) -> int: ...
418 def pensize(width: int) -> None: ...
419 def penup() -> None: ...
420 def pendown() -> None: ...
421 def isdown() -> bool: ...
423 def speed(speed: None = ...) -> int: ...
425 def speed(speed: _Speed) -> None: ...
427 def pencolor() -> _AnyColor: ...
429 def pencolor(color: _Color) -> None: ...
431 def pencolor(r: float, g: float, b: float) -> None: ...
433 def fillcolor() -> _AnyColor: ...
435 def fillcolor(color: _Color) -> None: ...
437 def fillcolor(r: float, g: float, b: float) -> None: ...
439 def color() -> tuple[_AnyColor, _AnyColor]: ...
441 def color(color: _Color) -> None: ...
443 def color(r: float, g: float, b: float) -> None: ...
445 def color(color1: _Color, color2: _Color) -> None: ...
446 def showturtle() -> None: ...
447 def hideturtle() -> None: ...
448 def isvisible() -> bool: ...
450 # Note: signatures 1 and 2 overlap unsafely when no arguments are provided
452 def pen() -> _PenState: ... # type: ignore
455 pen: _PenState | None = ...,
459 pencolor: _Color = ...,
460 fillcolor: _Color = ...,
463 resizemode: str = ...,
464 stretchfactor: tuple[float, float] = ...,
477 # Functions copied from RawTurtle:
479 def setundobuffer(size: int | None) -> None: ...
480 def undobufferentries() -> int: ...
482 def shape(name: None = ...) -> str: ...
484 def shape(name: str) -> None: ...
486 # Unsafely overlaps when no arguments are provided
488 def shapesize() -> tuple[float, float, float]: ... # type: ignore
490 def shapesize(stretch_wid: float | None = ..., stretch_len: float | None = ..., outline: float | None = ...) -> None: ...
492 def shearfactor(shear: None = ...) -> float: ...
494 def shearfactor(shear: float) -> None: ...
496 # Unsafely overlaps when no arguments are provided
498 def shapetransform() -> tuple[float, float, float, float]: ... # type: ignore
501 t11: float | None = ..., t12: float | None = ..., t21: float | None = ..., t22: float | None = ...
503 def get_shapepoly() -> _PolygonCoords | None: ...
504 def settiltangle(angle: float) -> None: ...
506 def tiltangle(angle: None = ...) -> float: ...
508 def tiltangle(angle: float) -> None: ...
509 def tilt(angle: float) -> None: ...
511 # Can return either 'int' or Tuple[int, ...] based on if the stamp is
512 # a compound stamp or not. So, as per the "no Union return" policy,
514 def stamp() -> Any: ...
515 def clearstamp(stampid: int | Tuple[int, ...]) -> None: ...
516 def clearstamps(n: int | None = ...) -> None: ...
517 def filling() -> bool: ...
518 def begin_fill() -> None: ...
519 def end_fill() -> None: ...
520 def dot(size: int | None = ..., *color: _Color) -> None: ...
521 def write(arg: object, move: bool = ..., align: str = ..., font: tuple[str, int, str] = ...) -> None: ...
522 def begin_poly() -> None: ...
523 def end_poly() -> None: ...
524 def get_poly() -> _PolygonCoords | None: ...
525 def getscreen() -> TurtleScreen: ...
526 def getturtle() -> Turtle: ...
530 def onrelease(fun: Callable[[float, float], Any], btn: int = ..., add: Any | None = ...) -> None: ...
531 def ondrag(fun: Callable[[float, float], Any], btn: int = ..., add: Any | None = ...) -> None: ...
532 def undo() -> None: ...
534 turtlesize = shapesize
536 # Functions copied from RawTurtle with a few tweaks:
538 def clone() -> Turtle: ...
540 # Extra functions present only in the global scope: