【问题标题】:VsCode don't run any image in pythonVsCode不在python中运行任何图像
【发布时间】:2022-07-19 05:32:38
【问题描述】:

我的 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

【问题讨论】:

  • “无法打开“olho.py”:没有这样的文件或目录”——那么,该文件是否存在于工作目录中?它实际上是一个图像文件吗?
  • 克里斯,我尝试对图像执行的所有操作都给我一个错误,我的图像在我的项目粘贴中
  • 这不能回答我的问题。工作目录中有一个名为olho.py 的文件吗?错误提示找不到该文件。
  • 修复图像路径后您可能会遇到的第二个问题是图像很少以.py 文件扩展名命名。该文件实际上是图像吗?文档说这个参数必须引用一个 GIF(或者是 "nopic"None)。
  • 对不起,我错过了帖子中的数字,但代码中是 .gif 图像

标签: python image visual-studio-code python-turtle


【解决方案1】:

我按照你写的尝试了,当我从工作区中删除文件时就会出现你说的问题。

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

【讨论】:

    【解决方案2】:
    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 可以工作。

    【讨论】:

      【解决方案3】:

      我也有同样的问题,Vscode中的python无法识别当前目录图片,但是当我使用完整路径时问题会解决

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

      ps。 您必须使用双 \ 进行转义

      【讨论】:

        猜你喜欢
        • 2015-09-23
        • 2015-07-10
        • 1970-01-01
        • 2020-02-21
        • 1970-01-01
        • 2017-09-04
        • 1970-01-01
        • 2023-03-15
        • 2022-07-19
        相关资源
        最近更新 更多