让乌龟改变它的形象

时间:2017-06-21 20:58:44

标签: python turtle-graphics

我有一些代码,但不断收到此错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\turtle.py", line 686, in eventfun
    fun()
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\RPG.py", line 21, in up
    combat()
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\RPG.py", line 58, in combat
    enemy.shape(image)
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\turtle.py", line 2777, in shape
    self.turtle._setshape(name)
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\turtle.py", line 2506, in _setshape
    self._item = screen._createimage(screen._shapes["blank"]._data)
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\turtle.py", line 723, in _createimage
    return self.cv.create_image(0, 0, image=image)
  File "<string>", line 1, in create_image
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2483, in create_image
    return self._create('image', args, kw)
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2474, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: image "pyimage1" doesn't exist
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\turtle.py", line 686, in eventfun
    fun()
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\RPG.py", line 21, in up
    combat()
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\RPG.py", line 58, in combat
    enemy.shape(image)
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\turtle.py", line 2777, in shape
    self.turtle._setshape(name)
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\turtle.py", line 2506, in _setshape
    self._item = screen._createimage(screen._shapes["blank"]._data)
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\turtle.py", line 723, in _createimage
    return self.cv.create_image(0, 0, image=image)
  File "<string>", line 1, in create_image
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2483, in create_image
    return self._create('image', args, kw)
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2474, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: image "pyimage1" doesn't exist

我的代码简化程度是

import os
from turtle import Turtle,Screen
print(os.getcwd())
os.chdir('C:\\Users\\Travi\\Downloads')
print(os.getcwd())
screen.register_shape("Crawfish_attack.gif")
Turtle.shape("Crawfish_attack.gif")

1 个答案:

答案 0 :(得分:0)

以这种方式尝试:

import os
from turtle import Turtle, Screen

print(os.getcwd())
os.chdir('C:\\Users\\Travi\\Downloads')
print(os.getcwd())

screen = Screen()
screen.register_shape('Crawfish_attack.gif')

crawfish = Turtle()
crawfish.shape('Crawfish_attack.gif')

screen.exitonclick()

您正在调用尚未创建的实例上的方法,并在类上调用实例方法。如果以上内容适合您,您还应该能够将这些结合起来:

crawfish = Turtle()
crawfish.shape('Crawfish_attack.gif')

成:

crawfish = Turtle('Crawfish_attack.gif')