Skip to content

Commit

Permalink
pythongh-95371: Add support for other image formats(e.g. PNG) to the …
Browse files Browse the repository at this point in the history
…turtle module
  • Loading branch information
relent95 committed Jul 28, 2022
1 parent bceb197 commit 1e30349
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
15 changes: 12 additions & 3 deletions Doc/library/turtle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ Window control

.. function:: bgpic(picname=None)

:param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
:param picname: a string, name of an image file(PGM, PPM, GIF, and PNG) or ``"nopic"``, or ``None``

Set background image or return name of current backgroundimage. If *picname*
is a filename, set the corresponding image as background. If *picname* is
Expand Down Expand Up @@ -1998,15 +1998,24 @@ Settings and special methods
Image shapes *do not* rotate when turning the turtle, so they do not
display the heading of the turtle!

(2) *name* is an arbitrary string and *shape* is a tuple of pairs of
(2) *name* is an arbitrary string and *shape* is the name of an image file(PGM, PPM, GIF, and PNG): Install the
corresponding image shape. ::

>>> screen.register_shape("turtle", "turtle.gif")

.. note::
Image shapes *do not* rotate when turning the turtle, so they do not
display the heading of the turtle!

(3) *name* is an arbitrary string and *shape* is a tuple of pairs of
coordinates: Install the corresponding polygon shape.

.. doctest::
:skipif: _tkinter is None

>>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))

(3) *name* is an arbitrary string and *shape* is a (compound) :class:`Shape`
(4) *name* is an arbitrary string and *shape* is a (compound) :class:`Shape`
object: Install the corresponding compound shape.

Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
Expand Down
30 changes: 14 additions & 16 deletions Lib/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
turtle. So the turtles can more easily be used as a visual feedback
instrument by the (beginning) programmer.
- Different turtle shapes, gif-images as turtle shapes, user defined
- Different turtle shapes, image files as turtle shapes, user defined
and user controllable turtle shapes, among them compound
(multicolored) shapes. Turtle shapes can be stretched and tilted, which
makes turtles very versatile geometrical objects.
Expand Down Expand Up @@ -474,7 +474,7 @@ def _blankimage(self):

def _image(self, filename):
"""return an image object containing the
imagedata from a gif-file named filename.
imagedata from an image file named filename.
"""
return TK.PhotoImage(file=filename, master=self.cv)

Expand Down Expand Up @@ -882,10 +882,7 @@ def __init__(self, type_, data=None):
if isinstance(data, list):
data = tuple(data)
elif type_ == "image":
if isinstance(data, str):
if data.lower().endswith(".gif") and isfile(data):
data = TurtleScreen._image(data)
# else data assumed to be Photoimage
assert(isinstance(data, TK.PhotoImage))
elif type_ == "compound":
data = []
else:
Expand Down Expand Up @@ -1110,14 +1107,18 @@ def register_shape(self, name, shape=None):
"""Adds a turtle shape to TurtleScreen's shapelist.
Arguments:
(1) name is the name of a gif-file and shape is None.
(1) name is the name of an image file(PGM, PPM, GIF, and PNG) and shape is None.
Installs the corresponding image shape.
!! Image-shapes DO NOT rotate when turning the turtle,
!! so they do not display the heading of the turtle!
(2) name is an arbitrary string and shape is a tuple
(2) name is an arbitrary string and shape is the name of an image file(PGM, PPM, GIF, and PNG).
Installs the corresponding image shape.
!! Image-shapes DO NOT rotate when turning the turtle,
!! so they do not display the heading of the turtle!
(3) name is an arbitrary string and shape is a tuple
of pairs of coordinates. Installs the corresponding
polygon shape
(3) name is an arbitrary string and shape is a
(4) name is an arbitrary string and shape is a
(compound) Shape object. Installs the corresponding
compound shape.
To use a shape, you have to issue the command shape(shapename).
Expand All @@ -1130,12 +1131,9 @@ def register_shape(self, name, shape=None):
"""
if shape is None:
# image
if name.lower().endswith(".gif"):
shape = Shape("image", self._image(name))
else:
raise TurtleGraphicsError("Bad arguments for register_shape.\n"
+ "Use help(register_shape)" )
shape = Shape("image", self._image(name))
elif isinstance(shape, str):
shape = Shape("image", self._image(shape))
elif isinstance(shape, tuple):
shape = Shape("polygon", shape)
## else shape assumed to be Shape-instance
Expand Down Expand Up @@ -1464,7 +1462,7 @@ def bgpic(self, picname=None):
"""Set background image or return name of current backgroundimage.
Optional argument:
picname -- a string, name of a gif-file or "nopic".
picname -- a string, name of an image file(PGM, PPM, GIF, and PNG) or "nopic".
If picname is a filename, set the corresponding image as background.
If picname is "nopic", delete backgroundimage, if present.
Expand Down

0 comments on commit 1e30349

Please sign in to comment.