[英]why is python tkinter module unable to find file even though there is one?
so I know you might have read similar questions but my question is just a bit different, I use python 3.8.3 and I know python basics so I wanted to start using GUI, I started off with Tkinter.所以我知道您可能已经阅读过类似的问题,但我的问题有点不同,我使用 python 3.8.3 并且我知道 python 基础知识,所以我想开始使用 GUI,我从 Z6F8BBEA5A81184EBA80DZ78919F51A60 开始。 I had written a small program which does nothing I was just trying but it doesn't work I don't know why.我写了一个小程序,它什么都不做,我只是在尝试,但它不起作用,我不知道为什么。 It gives error that unable to find file, I tried doing the code people told in other questions but it doesn't work, probably due to python versions, here is my code:它给出了无法找到文件的错误,我尝试执行人们在其他问题中告诉的代码,但它不起作用,可能是由于 python 版本,这是我的代码:
from tkinter import *
window= Tk()
window.title("first GUI program of mine")
window.configure(background="black")
photo1 = PhotoImage(file="images.jpg")
Label (window, text="1 or 2 ? :", bg="black", fg="white", font="calibri 12 bold") .grid(row=1, column=0, sticky=W)
windows.mainloop()
so when I run this I get error like this:所以当我运行它时,我得到这样的错误:
tkinter.TclError: couldn't open "images.jpg": no such file or directory
the file is in the same folder in which the python program is saved.该文件位于保存 python 程序的同一文件夹中。
any ideas?有任何想法吗?
Can you show your working directory?你能显示你的工作目录吗? I think it's the problem of directory.我认为这是目录的问题。 If it's under the same level of directory, maybe you could try this: '/images.jpg'如果它在同一级别的目录下,也许你可以试试这个:'/images.jpg'
PhotoImage class only supports GIF, PGM, PPM formats. PhotoImage class 仅支持 GIF、PGM、PPM 格式。 So try to convert it.所以尝试转换它。
That worked for me这对我有用
from tkinter import *
from PIL import Image, ImageTk
window= Tk()
window.title("first GUI program of mine")
window.configure()
photo1 = Image.open('images.jpeg')
photo1.show()
Label(window, text="1 or 2 ? :", bg="black", fg="white", font="calibri 12 bold").grid(row=1, column=0, sticky=W)
window.mainloop()
And where do you want the image to be shown?您希望在哪里显示图像? To show the image use show() function.要显示图像,请使用 show() function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.