_tkinter.TclError: couldn't open "ImmaginiDiProva/ImmagineProva1360x768.png": no such file or directory

177 views Asked by At

I'm back here with a new error that I can't solve... it's a strange error, idk, I put the images in the same directory of the program, in the directory specified in the code... Ah, I don't know, but this is all the code:

from tkinter import *
def toggle_fs():
    """
    Function that enables fullscreen for the window I create
    """
    state = False if root.attributes('-fullscreen') else True
    root.attributes('-fullscreen', state)
    if not state:
        root.geometry('300x300+100+100')


print("Inserisci il nome della foto: ") # Enter the name of the image -> "ImmagineProva"
nomeImg = input()


def getImg(nomeImg):
    """
    Function that obtaines the right image, because I have different images that are all the same picture but all are different in resolution (1360x768, 1920x1080...)
    """

    # detect the resolution of the monitor
    WIDTH, HEIGHT = root.winfo_screenwidth(), root.winfo_screenheight()

    # Takes the right image thanks to its name and screen resolution took before
    img = PhotoImage(file = "ImmaginiDiProva/" + nomeImg + str(WIDTH) + "x" + str(HEIGHT) + ".png")

    # All the images have the same name "ImmagineProva" with the relative resolution
    return img


root = Tk(screenName = "Prova")
root.attributes("-fullscreen", True)

canvas = Canvas(root, bg = "white")
canvas.pack(fill = BOTH, expand = True)

img = getImg(nomeImg)
canvas.create_image(0, 0, anchor = NW, image = img)

root.bind("<Escape>", toggle_fs())

mainloop()

I hope this is enough for you... I'm frustrated, I put the images everywhere and it doesn't recognize them, wtf O.o

2

There are 2 answers

0
Seintian On

I read the comments, so I post an image so you'll be able to see everything about the program, images...

An image of the folder of the program

Ah, I did what said the utent in the comment (I don't remember the nick and the page doesn't make me scroll up lol) -> import os; print(etc...) and raised the error FileNotFoundError: [WinError 3] Impossibile trovare il percorso specificato: 'ImmaginiDiProva'

Why this? there is also the folder in the image I posted... I don't understand ahahah

Thank you guys for your help, by the way <3

0
Peter On

I think, the problem is in tkinter, it disturbs the path because it has a problem with the backslash. My Error message:

Traceback (most recent call last):
  File "C:\python\koax.py", line 43, in <module>
    bild1 = tk.PhotoImage(file="c:\biene.png")
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 4125, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 4072, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "c:iene.png": no such file or directory

Line 3 shows the correct path, the last Line shows the resulting string ok tkinter. (working on Win 10 pro with python 3.11.5)