我带着一个无法解决的新错误回到这里。。。这是一个奇怪的错误,idk,我把图像放在程序的同一目录中,代码中指定的目录中。。。啊,我不知道,但这都是代码:
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()
我希望这对你来说已经足够了。。。我很沮丧,我把图像放在各处,它都认不出来,wtf O.O
我阅读了评论,所以我发布了一张图片,这样你就可以看到关于程序的一切,图片。。。
程序文件夹的图像
啊,我做了评论中说的话(我不记得缺口了,页面也不会让我向上滚动,哈哈(->import os; print(etc...)
并引发错误FileNotFoundError: [WinError 3] Impossibile trovare il percorso specificato: 'ImmaginiDiProva'
为什么这样?还有我发布的图片中的文件夹。。。我不懂啊哈哈
顺便说一句,谢谢你们的帮助<3
我认为问题出在tkinter中,它扰乱了路径,因为它的反斜杠有问题。我的错误消息:
Traceback (most recent call last):
File "C:pythonkoax.py", line 43, in <module>
bild1 = tk.PhotoImage(file="c:biene.png")
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0Libtkinter__init__.py", line 4125, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.11_3.11.1520.0_x64__qbz5n2kfra8p0Libtkinter__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
第3行显示了正确的路径,最后一行显示了结果字符串ok tkinter。(使用python 3.11.5在Win 10 pro上工作(