How should I solve the error _tkinter.TclError: couldn't open "sample.png": no such file or directory
Question:
I have my code inside the same folder as my image. And get error _tkinter.TclError: couldn't open "sample.png": no such file or directory
(I use visual studio code IDE)
I tried using PLE but that didn’t work.
import tkinter as tk
HEIGHT = 700
Width = 800
root = tk.Tk()
canvas = tk.Canvas(root, height = HEIGHT , width = Width)
canvas.pack()
background_image = tk.PhotoImage(file = "sample.png")
back_label = tk.Label(root,Image = background_image)
back_label.place(relwidth = 1 ,relheight = 1)
root.mainloop()
Answers:
First of all i think that you should use the whole image path (I am not sure but I am using ubuntu and I should use the full path). Then i change the code a bit and this works for me
import tkinter as tk
HEIGHT = 700
Width = 800
root = tk.Tk()
canvas = tk.Canvas(root, height = HEIGHT , width = Width)
canvas.pack()
background_image=tk.PhotoImage(file = "sample.png")
background_label = tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()
couldn’t open “sample.png”: no such file or directory means your file is not at the correct place you have 2 choices either you move it to where you launch it or you use an absolute path
It might also make a difference where you are executing the python script from if you are doing via terminal. If, for instance you are running a script from /home/usrname and executing something like >> ./downloads/src/run.py your relative path will be /home/usrname/, not /home/usrname/downloads/src/. If you navigate to that folder and try to run the python script it may suddenly find that image.
So the solution would be to use a fully qualified path for the image, or maybe there are better ways to set a relative path. idk
I had the same using building a tkinter app on my ubuntu. My issue was just the wrong file name.
Make sure your file name and file path are correct and you should be good to go.
import tkinter as tk
from PIL import Image, ImageTk
img = tk.PhotoImage(file="assets/logo.png")
logo_widget = tk.Label(frame1, image=img, bg=bg_color)
logo_widget.pack()
OR
img = ImageTk.PhotoImage(file="assets/logo.png")
logo_widget = tk.Label(frame1, image=img, bg=bg_color)
logo_widget.pack()
I have my code inside the same folder as my image. And get error _tkinter.TclError: couldn't open "sample.png": no such file or directory
(I use visual studio code IDE)
I tried using PLE but that didn’t work.
import tkinter as tk
HEIGHT = 700
Width = 800
root = tk.Tk()
canvas = tk.Canvas(root, height = HEIGHT , width = Width)
canvas.pack()
background_image = tk.PhotoImage(file = "sample.png")
back_label = tk.Label(root,Image = background_image)
back_label.place(relwidth = 1 ,relheight = 1)
root.mainloop()
First of all i think that you should use the whole image path (I am not sure but I am using ubuntu and I should use the full path). Then i change the code a bit and this works for me
import tkinter as tk
HEIGHT = 700
Width = 800
root = tk.Tk()
canvas = tk.Canvas(root, height = HEIGHT , width = Width)
canvas.pack()
background_image=tk.PhotoImage(file = "sample.png")
background_label = tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()
couldn’t open “sample.png”: no such file or directory means your file is not at the correct place you have 2 choices either you move it to where you launch it or you use an absolute path
It might also make a difference where you are executing the python script from if you are doing via terminal. If, for instance you are running a script from /home/usrname and executing something like >> ./downloads/src/run.py your relative path will be /home/usrname/, not /home/usrname/downloads/src/. If you navigate to that folder and try to run the python script it may suddenly find that image.
So the solution would be to use a fully qualified path for the image, or maybe there are better ways to set a relative path. idk
I had the same using building a tkinter app on my ubuntu. My issue was just the wrong file name.
Make sure your file name and file path are correct and you should be good to go.
import tkinter as tk
from PIL import Image, ImageTk
img = tk.PhotoImage(file="assets/logo.png")
logo_widget = tk.Label(frame1, image=img, bg=bg_color)
logo_widget.pack()
OR
img = ImageTk.PhotoImage(file="assets/logo.png")
logo_widget = tk.Label(frame1, image=img, bg=bg_color)
logo_widget.pack()