-1

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
  • "couldn't open "olho.py": no such file or directory"—well, does that file exist in the working directory? And is it actually an image file? – ChrisGPT was on strike Mar 09 '22 at 20:45
  • Chris, everything that i try to do with an image, give me an error, and my image is in my project paste – Tiago Medeiros Mar 09 '22 at 20:46
  • That doesn't answer my question. Is a file called `olho.py` in the working directory? The error says it can't find that file. – ChrisGPT was on strike Mar 09 '22 at 20:47
  • A second issue that you'll likely hit as soon as you fix your image path is that images are rarely named with `.py` file extensions. Is that file actually an image? The documentation says this argument must refer to a GIF (or be `"nopic"` or `None`). – ChrisGPT was on strike Mar 09 '22 at 20:48
  • sorry i miss digit in the post, but in the code is if a .gif image – Tiago Medeiros Mar 09 '22 at 20:51
  • What do you mean by "in the code is if a .gif image"? This is pretty straightforward: is `olho.py` the name of a GIF file in the working directory? – ChrisGPT was on strike Mar 09 '22 at 20:52
  • i edit the post – Tiago Medeiros Mar 09 '22 at 20:55
  • You've changed the name of the file, but the problem remains the same. Is that file _in the working directory_ or somewhere else? (Note that the documentation specifically says you must use a GIF. Trying a JPEG is unlikely to work.) You've tagged VSCode—how are you running this code? Do you know what your working directory is? – ChrisGPT was on strike Mar 09 '22 at 20:56
  • But the image is in my project directory – Tiago Medeiros Mar 09 '22 at 20:57
  • I'm running with bash, and i know the working directory is – Tiago Medeiros Mar 09 '22 at 20:59
  • "I'm running with bash"—how, _exactly?_ Are you doing `cd c:/Users/User/Desktop/ProgramasPython/Cursopython/Day20/`, then running `python Day20.py`? I suspect not—your error message shows full paths to both the Python interpreter and your script. If you run `cwd`, what does it show you? The directory you are in, the one that is printed out when you run `cwd`, is your working directory, and I strongly suspect it doesn't contain the GIF you are trying to use. – ChrisGPT was on strike Mar 10 '22 at 12:02
  • See https://stackoverflow.com/q/45591428/354577. Assuming that is the case, you can either `cd` to the right directory before running your script or update your script to [find the GIF relative to the location of the Python file](https://stackoverflow.com/q/4060221/354577) instead of the working directory. – ChrisGPT was on strike Mar 10 '22 at 12:08

3 Answers3

0

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.

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
0
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.

Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
0

Python in Vscode 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 double \\ for escape

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
X ZEX
  • 1
  • 1