-2

I use python3.7 and 8.6version of Tkinter.

I'm studying to use Tkinter and the problem occurred.

The code is

from tkinter import *

root = Tk()
root.title("Nado GUI")

photo = PhotoImage(file="image.png")
btn6 = Button(root, image = photo)
btn6.pack()
#작동안됨...

root.mainloop()

and the error is

/usr/local/anaconda3/envs/python_GUI/bin/python /Users/junil/PycharmProjects/Python_GUI/2_button.py Traceback (most recent call last): File "/Users/junil/PycharmProjects/Python_GUI/2_button.py", line 22, in photo = PhotoImage(file="image.png") File "/usr/local/anaconda3/envs/python_GUI/lib/python3.7/tkinter/init.py", line 3545, in init Image.init(self, 'photo', name, cnf, master, **kw) File "/usr/local/anaconda3/envs/python_GUI/lib/python3.7/tkinter/init.py", line 3501, in init self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "image.png": no such file or directory Process finished with exit code 1

I did use python2.7, PIL, and change to gif format but those didn't work.

Please help me to learn.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
  • 1
    The error means exactly what it says - it can't find "image.png" in the current working directory. – Bryan Oakley Jul 16 '20 at 15:42
  • 1
    It is not good practice to import APIs using `*` https://stackoverflow.com/questions/2386714/why-is-import-bad – r4bb1t Jul 16 '20 at 16:03
  • @PyWalker2797 I agree, but that's for longer code with more imports. Anyways, I highly doubt that's what's causing the issue. – 10 Rep Jul 16 '20 at 19:52
  • Yep, that's not causing the issue. But good practices are good practices! – r4bb1t Jul 16 '20 at 22:46

1 Answers1

0

The error states:

_tkinter.TclError: couldn't open "image.png": no such file or directory.

This obviously means that this image was not found in your directory. You might want to check two things:

  1. If your image file is present in the current working directory of your code or not.
  2. Verify your current working directory. Depending on that you might have to use the full path, you might also be able to get away with a relative path.
10 Rep
  • 2,217
  • 7
  • 19
  • 33
r4bb1t
  • 1,033
  • 2
  • 13
  • 36