0

What I am trying to do:

I am trying to insert an image in a Tkinter Window. For that, I checked both these threads:

How to add an image in Tkinter?

https://pythonprogramming.net/tkinter-adding-text-images/

After checking that, I tried implementing the ideas in my code.

A snippet of the code:

def Alimentacao():
    lb11=Label(janela, text="O seu consumo de alimentos:", font = ("Verdana", "16"))
    lb11.place(x = 60, y = 255)
    load = Image.open("Code.png")
    render = ImageTk.PhotoImage(load)
    img=Label(janela, image = render)
    img.place(x = 400, y = 400)

The error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Joao Zoioi\Anaconda3\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "<ipython-input-1-e4cd6e9966ba>", line 98, in Ocupacao
    Alimentacao()
  File "<ipython-input-1-e4cd6e9966ba>", line 103, in Alimentacao
    load = Image.open("Code.png")
  File "C:\Users\Joao Zoioi\Anaconda3\lib\site-packages\PIL\Image.py", line 2580, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Code.png'

What I think the issue is:

Apparently I can't just use the file's name, so I obviously need to have the file path somewhere, I just don't know where that is.

EDIT:

Before asking the question I did my research, as you can see. I went through this post as well:

Python can't open file "No such file or directory"

My question has been suggested to be a duplicate of this post. I disagree, as this other topic is very specific and hardly answers my concern.

J_Zoio
  • 230
  • 1
  • 2
  • 14
  • Put the file path in the Image.open(), however I recommend just using relative pathing. If you put the image in the same directory as your script that code should work. – I Funball Jan 08 '19 at 16:53
  • I do not recommend an implicit relative path at all. You put yourself at the mercy of your current working directory and that changes easily depending on how you execute the script. You might want to look for `os.path.dirname()` of your script and go relative from there. – r.ook Jan 08 '19 at 16:55
  • @Idlehands That's exactly what I was recommending? Did I phrase it wrong or something? – I Funball Jan 08 '19 at 16:57
  • There's a difference between using `Image.open('Code.png')` and `Image.open(directory + 'Code.png'))`. Unless you are suggesting the latter. The former is prone to error as `'Code.png'` will always be looking at the current working directory (`os.getwcd()`), whereas the latter you have control over where that relative path comes from. – r.ook Jan 08 '19 at 17:10
  • I disagree. Thank you for your inputs, though. – J_Zoio Jan 08 '19 at 17:20
  • Have you looked at this then: https://stackoverflow.com/questions/12201928/python-open-gives-ioerror-errno-2-no-such-file-or-directory – r.ook Jan 08 '19 at 17:33
  • This is definitely the duplicate of several questions. You're assuming a relative path is relative to where the script is, but that is not true. It's relative to the current working directory which may or may not be the same. – Bryan Oakley Jan 08 '19 at 18:06

0 Answers0