8000 gh-95371: Add support for other image formats(e.g. PNG) to the turtle… · python/cpython@e1baa77 · GitHub
[go: up one dir, main page]

Skip to content

Commit e1baa77

Browse files
relent95hugovkblurb-it[bot]arhadthedev
authored
gh-95371: Add support for other image formats(e.g. PNG) to the turtle… (#95378)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
1 parent 60c6518 commit e1baa77

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

Doc/library/turtle.rst

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,8 @@ Window control
18231823

18241824
.. function:: bgpic(picname=None)
18251825

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

18281829
Set background image or return name of current backgroundimage. If *picname*
18291830
is a filename, set the corresponding image as background. If *picname* is
@@ -2200,9 +2201,9 @@ Settings and special methods
22002201
.. function:: register_shape(name, shape=None)
22012202
addshape(name, shape=None)
22022203

2203-
There are three different ways to call this function:
2204+
There are four different ways to call this function:
22042205

2205-
(1) *name* is the name of a gif-file and *shape* is ``None``: Install the
2206+
(1) *name* is the name of an image file (PNG, GIF, PGM, and PPM) and *shape* is ``None``: Install the
22062207
corresponding image shape. ::
22072208

22082209
>>> screen.register_shape("turtle.gif")
@@ -2211,20 +2212,33 @@ Settings and special methods
22112212
Image shapes *do not* rotate when turning the turtle, so they do not
22122213
display the heading of the turtle!
22132214

2214-
(2) *name* is an arbitrary string and *shape* is a tuple of pairs of
2215+
(2) *name* is an arbitrary string and *shape* is the name of an image file (PNG, GIF, PGM, and PPM): Install the
2216+
corresponding image shape. ::
2217+
2218+
>>> screen.register_shape("turtle", "turtle.gif")
2219+
2220+
.. note::
2221+
Image shapes *do not* rotate when turning the turtle, so they do not
2222+
display the heading of the turtle!
2223+
2224+
(3) *name* is an arbitrary string and *shape* is a tuple of pairs of
22152225
coordinates: Install the corresponding polygon shape.
22162226

22172227
.. doctest::
22182228
:skipif: _tkinter is None
22192229

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

2222-
(3) *name* is an arbitrary string and *shape* is a (compound) :class:`Shape`
2232+
(4) *name* is an arbitrary string and *shape* is a (compound) :class:`Shape`
22232233
object: Install the corresponding compound shape.
22242234

22252235
Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
22262236
shapes can be used by issuing the command ``shape(shapename)``.
22272237

2238+
.. versionchanged:: next
2239+
Added support for PNG, PGM, and PPM image formats.
2240+
Both a shape name and an image file name can be specified.
2241+
22282242

22292243
.. function:: turtles()
22302244

Lib/turtle.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
turtle. So the turtles can more easily be used as a visual feedback
5252
instrument by the (beginning) programmer.
5353
54-
- Different turtle shapes, gif-images as turtle shapes, user defined
54+
- Different turtle shapes, image files as turtle shapes, user defined
5555
and user controllable turtle shapes, among them compound
5656
(multicolored) shapes. Turtle shapes can be stretched and tilted, which
5757
makes turtles very versatile geometrical objects.
@@ -468,7 +468,7 @@ def _blankimage(self):
468468

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

@@ -872,10 +872,7 @@ def __init__(self, type_, data=None):
872872
if isinstance(data, list):
873873
data = tuple(data)
874874
elif type_ == "image":
875-
if isinstance(data, str):
876-
if data.lower().endswith(".gif") and isfile(data):
877-
data = TurtleScreen._image(data)
878-
# else data assumed to be PhotoImage
875+
assert(isinstance(data, TK.PhotoImage))
879876
elif type_ == "compound":
880877
data = []
881878
else:
@@ -1100,14 +1097,18 @@ def register_shape(self, name, shape=None):
11001097
"""Adds a turtle shape to TurtleScreen's shapelist.
11011098
11021099
Arguments:
1103-
(1) name is the name of a gif-file and shape is None.
1100+
(1) name is the name of an image file (PNG, GIF, PGM, and PPM) and shape is None.
11041101
Installs the corresponding image shape.
11051102
!! Image-shapes DO NOT rotate when turning the turtle,
11061103
!! so they do not display the heading of the turtle!
1107-
(2) name is an arbitrary string and shape is a tuple
1104+
(2) name is an arbitrary string and shape is the name of an image file (PNG, GIF, PGM, and PPM).
1105+
Installs the corresponding image shape.
1106+
!! Image-shapes DO NOT rotate when turning the turtle,
1107+
!! so they do not display the heading of the turtle!
1108+
(3) name is an arbitrary string and shape is a tuple
11081109
of pairs of coordinates. Installs the corresponding
11091110
polygon shape
1110-
(3) name is an arbitrary string and shape is a
1111+
(4) name is an arbitrary string and shape is a
11111112
(compound) Shape object. Installs the corresponding
11121113
compound shape.
11131114
To use a shape, you have to issue the command shape(shapename).
@@ -1120,12 +1121,9 @@ def register_shape(self, name, shape=None):
11201121
11211122
"""
11221123
if shape is None:
1123-
# image
1124-
if name.lower().endswith(".gif"):
1125-
shape = Shape("image", self._image(name))
1126-
else:
1127-
raise TurtleGraphicsError("Bad arguments for register_shape.\n"
1128-
+ "Use help(register_shape)" )
1124+
shape = Shape("image", self._image(name))
1125+
elif isinstance(shape, str):
1126+
shape = Shape("image", self._image(shape))
11291127
elif isinstance(shape, tuple):
11301128
shape = Shape("polygon", shape)
11311129
## else shape assumed to be Shape-instance
@@ -1454,7 +1452,7 @@ def bgpic(self, picname=None):
14541452
"""Set background image or return name of current backgroundimage.
14551453
14561454
Optional argument:
1457-
picname -- a string, name of a gif-file or "nopic".
1455+
picname -- a string, name of an image file (PNG, GIF, PGM, and PPM) or "nopic".
14581456
14591457
If picname is a filename, set the corresponding image as background.
14601458
If picname is "nopic", delete backgroundimage, if present.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for other image formats (PNG, PGM, and PPM) to the turtle module. Patch by Shin-myoung-serp.

0 commit comments

Comments
 (0)
0