如何将乌龟设置为图像

how do I set a turtle to an image

我正在尝试将我的图像设置为一个文件,但是当我 运行 它时,我得到

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 20, in up combat() File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-32\RPG.py", line 57, 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 "", 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')
screen.register_shape("Crawfish_attack.gif")
turtle = Turtle()
turtle.setimage("Crawfish_attack.gif")

提前致谢 顺便说一句 link 是 here 其余代码都可以正常工作,不需要显示

os.chdir() 参数中删除 "\Crawfish_attack"。您需要指向它所在文件夹的路径,而不是文件。这就是 register_shape 函数将执行的操作,即提取该文件夹中的确切文件。

您收到错误是因为您试图找到不存在的路径(它正在寻找一个文件夹)。

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