VsCode 不运行 python 中的任何图像

发布于 2025-01-12 19:46:41 字数 1469 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖凝香 2025-01-19 19:46:41

VCSode 中的 Python 无法识别当前目录图像,但当我使用完整路径时,问题就会解决。

icon = PhotoImage(file="E:\\\Python Project\\\Vs Code\\\GUI Tkinter\\\Icon.png")

附:
你必须使用 trible \\\ 来转义。

Python in VCSode does not recognize current directory images, but when I use the complete path the problem will resolve.

icon = PhotoImage(file="E:\\\Python Project\\\Vs Code\\\GUI Tkinter\\\Icon.png")

PS:
You got to use trible \\\ for escape.

天气好吗我好吗 2025-01-19 19:46:41

我按照你写的那样尝试过,当我从工作区中删除文件时,你所说的问题就会发生。

确保文件名与代码中的文件名一致,以及该文件是否在工作区中。

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.

暮年慕年 2025-01-19 19:46:41
import os
cwd = os.getcwd()   //Return a string representing the current working directory.

你可以通过上面的代码来获取该文件在python解释器中的搜索位置。

VSCode 中的 cwd 默认值为 workspace 文件夹(您在 VSCode 中打开的文件夹),如果 olho.gif 没有不在workspace文件夹下,python解释器找不到它。比如Day20文件夹下。

但是您可以将其添加到settings.json 文件中,将cwd 修改为
执行的 python 脚本的父文件夹。

"python.terminal.executeInFileDir": true,

那么如果Day20下的olho.gif可以工作。

import os
cwd = os.getcwd()   //Return a string representing the current working directory.

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 the workspace folder(the folder you opened in the VSCode), if the olho.gif does not under the workspace folder, the python interpreter can not find it. Such as it under the Day20 folder.

But you can add this in the settings.json file to modify the cwd to the
parent folder of your executed python script.

"python.terminal.executeInFileDir": true,

Then if the olho.gif under the Day20 will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文