对我来说,我想使用 PNG,而不是 GIF。终于找到了答案,把GIF和PNG的解决方法记录如下,希望对大家有所帮助。
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')
如何设置形状
首先,您需要register_shape by turtle.Screen(),
等等!开始之前,先看源码,如下:
# 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
好的,然后开始,
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)