Python海龟问题:无法打开gif(没有这样的文件或目录)

问题描述 投票:0回答:1

我尝试使用

.gif
库在 Python 表单上插入一些
turtle
图片。图片名称是
nindza.gif
。为了在窗口窗体上获取图片,我使用函数
bgpic()

我只是让你知道我尝试处理的图片与 Python 文件位于同一目录中。我必须提到的是,我红色了有关上述问题的所有评论。我已阅读所有说明,但没有任何帮助我解决问题。

这是代码:

import turtle

wn = turtle.Screen()

# Set the background image to nindza_kornjace.gif
wn.setup(width=600, height=400)
wn.bgpic("nindza.gif")

# exit on click
wn.exitonclick()

每次运行代码时,我都会不断收到以下错误:

File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 4072,
in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "nindza_kornjace.gif": no such file or directory

我想再说明一件事。当我进行相对文件路径寻址时,在相对文件路径之前使用bgpic函数中指定的前缀

r
,我可以打开图像。下一个代码工作:
wn.bgpic(r"Nedelja6\nindza.gif")

我想知道为什么程序可以使用相对寻址运行,但在仅指定引号下的文件名时却不能运行(

wn.bgpci("nindza.gif")
)。

python turtle-graphics
1个回答
0
投票

如果您还没有尝试过使用绝对路径,请尝试一下。只需指向“my_pc/user/xxx/folder/nindza.gif”之类的文件

© www.soinside.com 2019 - 2024. All rights reserved.