VsCode 不运行 python 中的任何图像
我的 VsCode 没有运行任何图像,我不知道为什么,jpg 也会发生这种情况。 这是我的代码:
from turtle import Turtle, Screen
screen=Screen()
screen.setup(600,600)
screen.bgpic(picname="olho.gif")
screen.exitonclick()
错误是:
$ c:/Users/User/Desktop/ProgramasPython/venv/Scripts/python.exe c:/Users/User/Desktop/ProgramasPython/Cursopython/Day20/Day20.py
Traceback (most recent call last):
File "c:\Users\User\Desktop\ProgramasPython\Cursopython\Day20\Day20.py", line 5, in <module>
screen.bgpic(picname="olho.gif")
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\turtle.py", line
1482, in bgpic
self._bgpics[picname] = self._image(picname)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\turtle.py", line
478, in _image
return TK.PhotoImage(file=filename, master=self.cv)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 4064, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 4009, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "olho.gif": no such file or directory
My VsCode isn't running any image, i don't know why, and this happens with jpg too.
Here is my code:
from turtle import Turtle, Screen
screen=Screen()
screen.setup(600,600)
screen.bgpic(picname="olho.gif")
screen.exitonclick()
And the error is:
$ c:/Users/User/Desktop/ProgramasPython/venv/Scripts/python.exe c:/Users/User/Desktop/ProgramasPython/Cursopython/Day20/Day20.py
Traceback (most recent call last):
File "c:\Users\User\Desktop\ProgramasPython\Cursopython\Day20\Day20.py", line 5, in <module>
screen.bgpic(picname="olho.gif")
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\turtle.py", line
1482, in bgpic
self._bgpics[picname] = self._image(picname)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\turtle.py", line
478, in _image
return TK.PhotoImage(file=filename, master=self.cv)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 4064, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 4009, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "olho.gif": no such file or directory
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
VCSode 中的 Python 无法识别当前目录图像,但当我使用完整路径时,问题就会解决。
附:
你必须使用 trible
\\\
来转义。Python in VCSode does not recognize current directory images, but when I use the complete path the problem will resolve.
PS:
You got to use trible
\\\
for escape.我按照你写的那样尝试过,当我从工作区中删除文件时,你所说的问题就会发生。
确保文件名与代码中的文件名一致,以及该文件是否在工作区中。
I tried as you wrote, and the problem you said will happen when I remove the file from the workspace.
Make sure that file name is consistent with the file name in the code, and whether the file is in the workspace.
你可以通过上面的代码来获取该文件在python解释器中的搜索位置。
VSCode 中的
cwd
默认值为workspace
文件夹(您在 VSCode 中打开的文件夹),如果olho.gif
没有不在workspace文件夹下,python解释器找不到它。比如Day20
文件夹下。但是您可以将其添加到settings.json 文件中,将
cwd
修改为执行的 python 脚本的父文件夹。
那么如果
Day20
下的olho.gif
可以工作。You can through the above code to get the search location of the file in the python interpreter.
The default value of the
cwd
in the VSCode was theworkspace
folder(the folder you opened in the VSCode), if theolho.gif
does not under the workspace folder, the python interpreter can not find it. Such as it under theDay20
folder.But you can add this in the settings.json file to modify the
cwd
to theparent folder of your executed python script.
Then if the
olho.gif
under theDay20
will work.