My python GUI-tkinter logo is not changing. I tried all of the methods. It continuously showing (couldn't open "logo.png": no such file or directory)

Viewed 28
from tkinter import *

windows = Tk()
windows.title("My Project")
windows.geometry('600x400')

icon  = PhotoImage(file='logo.png')
windows.iconphoto(True,icon)

# windows.iconbitmap('mylogo.ico')

windows.mainloop()

Error :

Traceback (most recent call last):
  File "e:\PROGRAMMING\Code in VS code\CODES\PROJECT GUI\gui_project.py", line 6, in <module>
    icon  = PhotoImage(file='logo.png')
  File "C:\Users\Hp\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Hp\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4038, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "logo.png": no such file or directory

Here image also added in gui 's folder . I tried direct path also and ico logo too. But failed.

1 Answers

These problems usually occur when you have opened the terminal in the parent directory and the image is inside a child directory. A quick fix would be to take the relative path of the image file and replace it in your code.

This problem can occur again if the terminal is opened in another location as opposed to the directory which contains source code.

For example: Consider the following directory structure

my_source_code > mypythonscript.py

my_source_code > images > logo.png

If you try to open a terminal in the "my_source_code" folder and run the .py file, it will not run as the required path is

"images/logo.png"

Related