使用图像作为乌龟形状

时间:2018-08-06 17:58:23

标签: python

我一直在努力尝试一些可能非常简单的事情。 我正在尝试将图像插入为乌龟形状,但错误始终存在。

我的代码是

 import turtle

 shape_2 = turtle.Shape('image', 'exemple.gif')
 turtle.register_shape('play', shape_2)
 turtle.shape('play')
 turtle.forward(100)
 turtle.left(45)
 turtle.forward(50)

错误是

[evaluate testee.py]
Traceback (most recent call last):
  File "C:/Users/Utilizador/Desktop/Python/testee.py", line 3, in <module>
    shape_2 = turtle.Shape('image', 'exemple.gif')
  File "C:\Users\Utilizador\Anaconda3\Lib\turtle.py", line 886, in __init__
    data = TurtleScreen._image(data)
  File "C:\Users\Utilizador\Anaconda3\Lib\turtle.py", line 479, in _image
    return TK.PhotoImage(file=filename)
  File "C:\Users\Utilizador\Anaconda3\Lib\tkinter\__init__.py", line 3539, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Utilizador\Anaconda3\Lib\tkinter\__init__.py", line 3483, in __init__
    raise RuntimeError('Too early to create image')
builtins.RuntimeError: Too early to create image

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以直接注册.gif文件形状:

import turtle

play = 'example.gif'

turtle.register_shape(play)
turtle.shape(play)
turtle.forward(100)
turtle.left(45)
turtle.forward(50)