mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
gh-65772: Clean-up turtle module (#104218)
* Remove the unused, private, and undocumented name `_ver` and the commented-out `print` call. * Don't add math functions to `__all__`. Beginners should learn to `import math` to access them. * Gregor Lindel, who wrote this version of turtle, dropped plans to implement turtle on another toolkit at least a decade ago. Drop `_dot` code preparing for this, but add a hint comment. * `_Screen` is meant to be a singleton class. To enforce that, it needs either a `__new__` that returns the singleton or `else...raise` in `__iter__`. Merely removing the `if` clauses as suggested might break something if a user were to call `_Screen` directly. Leave the code alone until a problem is evident. * Turtledemo injects into _Screen both _root and _canvas, configured as it needs them to be. Making _canvas an `__init__` option would require skipping some but not all of the lines under 'if _Screen._canvas is None:`. Leave working code alone.
This commit is contained in:
parent
6616710731
commit
e407661e7a
2 changed files with 22 additions and 40 deletions
|
@ -21,7 +21,6 @@
|
||||||
# misrepresented as being the original software.
|
# misrepresented as being the original software.
|
||||||
# 3. This notice may not be removed or altered from any source distribution.
|
# 3. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Turtle graphics is a popular way for introducing programming to
|
Turtle graphics is a popular way for introducing programming to
|
||||||
kids. It was part of the original Logo programming language developed
|
kids. It was part of the original Logo programming language developed
|
||||||
|
@ -97,13 +96,8 @@ Roughly it has the following features added:
|
||||||
|
|
||||||
Behind the scenes there are some features included with possible
|
Behind the scenes there are some features included with possible
|
||||||
extensions in mind. These will be commented and documented elsewhere.
|
extensions in mind. These will be commented and documented elsewhere.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_ver = "turtle 1.1b- - for Python 3.1 - 4. 5. 2009"
|
|
||||||
|
|
||||||
# print(_ver)
|
|
||||||
|
|
||||||
import tkinter as TK
|
import tkinter as TK
|
||||||
import types
|
import types
|
||||||
import math
|
import math
|
||||||
|
@ -141,7 +135,7 @@ _tg_turtle_functions = ['back', 'backward', 'begin_fill', 'begin_poly', 'bk',
|
||||||
_tg_utilities = ['write_docstringdict', 'done']
|
_tg_utilities = ['write_docstringdict', 'done']
|
||||||
|
|
||||||
__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
|
__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
|
||||||
_tg_utilities + ['Terminator']) # + _math_functions)
|
_tg_utilities + ['Terminator'])
|
||||||
|
|
||||||
_alias_list = ['addshape', 'backward', 'bk', 'fd', 'ht', 'lt', 'pd', 'pos',
|
_alias_list = ['addshape', 'backward', 'bk', 'fd', 'ht', 'lt', 'pd', 'pos',
|
||||||
'pu', 'rt', 'seth', 'setpos', 'setposition', 'st',
|
'pu', 'rt', 'seth', 'setpos', 'setposition', 'st',
|
||||||
|
@ -598,9 +592,6 @@ class TurtleScreenBase(object):
|
||||||
x0, y0, x1, y1 = self.cv.bbox(item)
|
x0, y0, x1, y1 = self.cv.bbox(item)
|
||||||
return item, x1-1
|
return item, x1-1
|
||||||
|
|
||||||
## def _dot(self, pos, size, color):
|
|
||||||
## """may be implemented for some other graphics toolkit"""
|
|
||||||
|
|
||||||
def _onclick(self, item, fun, num=1, add=None):
|
def _onclick(self, item, fun, num=1, add=None):
|
||||||
"""Bind fun to mouse-click event on turtle.
|
"""Bind fun to mouse-click event on turtle.
|
||||||
fun must be a function with two arguments, the coordinates
|
fun must be a function with two arguments, the coordinates
|
||||||
|
@ -2726,7 +2717,7 @@ class RawTurtle(TPen, TNavigator):
|
||||||
if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
|
if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
|
||||||
raise TurtleGraphicsError("bad color sequence: %s" % str(args))
|
raise TurtleGraphicsError("bad color sequence: %s" % str(args))
|
||||||
return "#%02x%02x%02x" % (r, g, b)
|
return "#%02x%02x%02x" % (r, g, b)
|
||||||
|
|
||||||
def teleport(self, x=None, y=None, *, fill_gap: bool = False) -> None:
|
def teleport(self, x=None, y=None, *, fill_gap: bool = False) -> None:
|
||||||
"""Instantly move turtle to an absolute position.
|
"""Instantly move turtle to an absolute position.
|
||||||
|
|
||||||
|
@ -2738,14 +2729,14 @@ class RawTurtle(TPen, TNavigator):
|
||||||
call: teleport(x, y) # two coordinates
|
call: teleport(x, y) # two coordinates
|
||||||
--or: teleport(x) # teleport to x position, keeping y as is
|
--or: teleport(x) # teleport to x position, keeping y as is
|
||||||
--or: teleport(y=y) # teleport to y position, keeping x as is
|
--or: teleport(y=y) # teleport to y position, keeping x as is
|
||||||
--or: teleport(x, y, fill_gap=True)
|
--or: teleport(x, y, fill_gap=True)
|
||||||
# teleport but fill the gap in between
|
# teleport but fill the gap in between
|
||||||
|
|
||||||
Move turtle to an absolute position. Unlike goto(x, y), a line will not
|
Move turtle to an absolute position. Unlike goto(x, y), a line will not
|
||||||
be drawn. The turtle's orientation does not change. If currently
|
be drawn. The turtle's orientation does not change. If currently
|
||||||
filling, the polygon(s) teleported from will be filled after leaving,
|
filling, the polygon(s) teleported from will be filled after leaving,
|
||||||
and filling will begin again after teleporting. This can be disabled
|
and filling will begin again after teleporting. This can be disabled
|
||||||
with fill_gap=True, which makes the imaginary line traveled during
|
with fill_gap=True, which makes the imaginary line traveled during
|
||||||
teleporting act as a fill barrier like in goto(x, y).
|
teleporting act as a fill barrier like in goto(x, y).
|
||||||
|
|
||||||
Example (for a Turtle instance named turtle):
|
Example (for a Turtle instance named turtle):
|
||||||
|
@ -2773,7 +2764,7 @@ class RawTurtle(TPen, TNavigator):
|
||||||
self._position = Vec2D(new_x, new_y)
|
self._position = Vec2D(new_x, new_y)
|
||||||
self.pen(pendown=pendown)
|
self.pen(pendown=pendown)
|
||||||
if was_filling and not fill_gap:
|
if was_filling and not fill_gap:
|
||||||
self.begin_fill()
|
self.begin_fill()
|
||||||
|
|
||||||
def clone(self):
|
def clone(self):
|
||||||
"""Create and return a clone of the turtle.
|
"""Create and return a clone of the turtle.
|
||||||
|
@ -3455,27 +3446,22 @@ class RawTurtle(TPen, TNavigator):
|
||||||
if size is None:
|
if size is None:
|
||||||
size = self._pensize + max(self._pensize, 4)
|
size = self._pensize + max(self._pensize, 4)
|
||||||
color = self._colorstr(color)
|
color = self._colorstr(color)
|
||||||
if hasattr(self.screen, "_dot"):
|
# If screen were to gain a dot function, see GH #104218.
|
||||||
item = self.screen._dot(self._position, size, color)
|
pen = self.pen()
|
||||||
self.items.append(item)
|
if self.undobuffer:
|
||||||
if self.undobuffer:
|
self.undobuffer.push(["seq"])
|
||||||
self.undobuffer.push(("dot", item))
|
self.undobuffer.cumulate = True
|
||||||
else:
|
try:
|
||||||
pen = self.pen()
|
if self.resizemode() == 'auto':
|
||||||
if self.undobuffer:
|
self.ht()
|
||||||
self.undobuffer.push(["seq"])
|
self.pendown()
|
||||||
self.undobuffer.cumulate = True
|
self.pensize(size)
|
||||||
try:
|
self.pencolor(color)
|
||||||
if self.resizemode() == 'auto':
|
self.forward(0)
|
||||||
self.ht()
|
finally:
|
||||||
self.pendown()
|
self.pen(pen)
|
||||||
self.pensize(size)
|
if self.undobuffer:
|
||||||
self.pencolor(color)
|
self.undobuffer.cumulate = False
|
||||||
self.forward(0)
|
|
||||||
finally:
|
|
||||||
self.pen(pen)
|
|
||||||
if self.undobuffer:
|
|
||||||
self.undobuffer.cumulate = False
|
|
||||||
|
|
||||||
def _write(self, txt, align, font):
|
def _write(self, txt, align, font):
|
||||||
"""Performs the writing for write()
|
"""Performs the writing for write()
|
||||||
|
@ -3751,11 +3737,6 @@ class _Screen(TurtleScreen):
|
||||||
_title = _CFG["title"]
|
_title = _CFG["title"]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# XXX there is no need for this code to be conditional,
|
|
||||||
# as there will be only a single _Screen instance, anyway
|
|
||||||
# XXX actually, the turtle demo is injecting root window,
|
|
||||||
# so perhaps the conditional creation of a root should be
|
|
||||||
# preserved (perhaps by passing it as an optional parameter)
|
|
||||||
if _Screen._root is None:
|
if _Screen._root is None:
|
||||||
_Screen._root = self._root = _Root()
|
_Screen._root = self._root = _Root()
|
||||||
self._root.title(_Screen._title)
|
self._root.title(_Screen._title)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Remove unneeded comments and code in turtle.py.
|
Loading…
Add table
Add a link
Reference in a new issue