简体   繁体   English

为什么 python tkinter 模块即使有文件也找不到文件?

[英]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.

相关问题 VS 代码:Python 程序正确调试,但在 output 中显示“无法找到 python 模块”,即使我没有导入任何东西 - VS Code: Python program debugs properly, but shows "Unable to find python module" in the output, even though I am not importing anything 即使正确安装,Python3仍找不到模块 - Python3 won't find a module even though it is properly installed 即使创建了模块,也找不到 Python 模块 - Python module not found even though module was created 为什么覆盖运行找不到 flask 模块,即使 unittest 可以找到它? - Why does coverage run not find flask module, even though unittest can find it? Python 模块已安装也找不到 - Python Module Can Not be Found Even Though It Is Installed Python:即使目录中存在模块,也未找到 - Python: No module found even though it exists in directory gcc - 即使 /Library/Frameworks/Python.framework 存在也无法找到 Python.framework? - gcc - Unable to find Python.framework even though /Library/Frameworks/Python.framework exists? 即使模块/文件明确存在,ModuleNotFoundError - ModuleNotFoundError even though module/file clearly exists Python 无法导入“nbt”,即使已安装 - Python Unable to import 'nbt' even though it is installed Python3 找不到 pip3 安装的模块,即使它安装了 defintley - Python3 cannot find module installed by pip3 even though it is deffintley installed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM