For me, I want to use PNG, not GIF. Finally, I found the answer and recorded both GIF and PNG solution as follows, hoping to help someone.
t = turtle.Turtle()
# you can change the shape with this command, but you need to tell it what the 'your_shape_name' is.
t.shape('your_shape_name')
How to set shape
First, You need register_shape
by turtle.Screen()
,
Wait! Before you start, see the source code first, as below:
# Lib/turtle.py
class TurtleScreen(TurtleScreenBase):
...
def register_shape(self, name, shape=None):
if shape is None:
# image
if name.lower().endswith(".gif"):
shape = Shape("image",
self._image(name) # return ``TK.PhotoImage(file=filename)`
)
else:
raise TurtleGraphicsError("Bad arguments for register_shape.\n"
+ "Use help(register_shape)" )
elif isinstance(shape, tuple):
shape = Shape("polygon", shape)
## else shape assumed to be Shape-instance
self._shapes[name] = shape
...
addshape = register_shape # <---- so they are the same
OK, then will start,
screen = turtle.Screen()
t = turtle.Turtle() # a Turtle instance
# t.shape('your_shape_name') # you can change the shape with this command, but you need tell it what is 'your_shape_name'
if '1. source is png (or anything that is compatible with `tk.PhotoImage`)':
your_shape=turtle.Shape('image', tk.PhotoImage(file='your.png'))) # name must is 'image'
screen.register_shape(name='your_cool_shape_name', shape=your_shape)
t.shape('your_cool_shape_name')
# t.goto(1, 2)
if '2 source is gif':
from pathlib import Path
your_gif = str(Path('.../your.gif'))
screen.register_shape(your_gif)
t.shape(your_gif)