1

for some reason, Tkinter can't open my image. If I don't add from tkinter import * it shows error message as:

Error Message without from tkinter import *:

C:\Users\NG>python e:/PythonTkinter/app.py
Traceback (most recent call last):
  File "e:/PythonTkinter/app.py", line 12, in <module>
    logo = Image.open('logo.png')
  File "C:\Users\NG\anaconda3\lib\site-packages\PIL\Image.py", line 2891, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'logo.png'

and if I add from tkinter import * as shown below, it shows error message as shown below.

Code:

import tkinter as tk
import PyPDF2
from PIL import Image, ImageTk
from tkinter import *

# begaining of our UI window
root = tk.Tk()

canvas = tk.Canvas(root, width=600, height=300)
canvas.grid(columnspan=3)

# Adding logo
# logo = ImageTk.PhotoImage(Image.open("logo.png"))
logo = Image.open("logo.png")
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image=logo)
loogo_label.image = logo
logo_label.grid(column=1, row=0)

# ending of our UI window
root.mainloop()

Error Message with from tkinter import *:

C:\Users\NG>python e:/PythonTkinter/app.py
Traceback (most recent call last):
  File "e:/PythonTkinter/app.py", line 14, in <module>
    logo = Image.open("logo.png")
AttributeError: type object 'Image' has no attribute 'open'

Image is right there in same folder where this python file is.

enter image description here

WHAT AM I DOING WRONG ? HELP!

kissu
  • 40,416
  • 14
  • 65
  • 133
Shrez Ean
  • 19
  • 3
  • For the second error, your imports are messed up. Either remove `from tkinter import *` or move it to top. – Delrius Euphoria Mar 14 '21 at 15:34
  • 1
    In the second case it's because of the `from tkinter import *`. There's a `tkinter.Image` which replaces the one from PIL. – martineau Mar 14 '21 at 15:39
  • Copied and pasted doesn't work for me. I had to type the script same as above. Then execute and it will work right now. **So don't try copy and paste.** – toyota Supra Jun 12 '23 at 20:26

3 Answers3

2

Explanation:-

You should probably understand what relative path is. When relative path is used, it is not relative the location of the python file, but instead the location from where you are running the python file. Here you are running the file from:

C:\Users\NG

But your python file and the image you are using is inside:

e:/PythonTkinter/app.py

Solution:-

So here either you can change the location from where you're running the code to the location with image file OR you can copy the image to the location you are running the py file from(i.e., 'C:\Users\NG').


And as far as the second error is concerned, it's never a good idea to say from x import *. When you import '*' from tkinter, it replaces the PIL.Image with tkinter.Image. And hence the error. So either remove that line or move it to the top most. Recommended import is:

import tkinter as tk
import PyPDF2
from PIL import Image, ImageTk

You are just using the first import here, so I don't see a point in using from tkinter import *, so just remove it.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
0

You can simply do as follows

from tkinter import *

window = Tk() # instantiate an instance of a window for us
window.geometry("500x500")

#creating a photo image from png photo
icon = PhotoImage(file='GUI using Python\\flower.png')
window.iconphoto(True, icon)

window.mainloop()

starting from the comment (creating a photo image from png photo), you give a value in the file parameter the relative path not the full path, also take care of the backslash, you should write two backslashes and no need for all of these libraries that you are using unless you are counting on them for other tasks.

-1

Your PIL import should be after 'from tkinter import *'. Also make sure to be in the same directory as the image

Omid Ki
  • 155
  • 3
  • 13
  • The order of imports don't matter unless you use wildcard `*` and those modules have classes with the same name. – astqx Mar 14 '21 at 16:12