首页 > 解决方案 > 为什么 python tkinter 模块即使有文件也找不到文件?

问题描述

所以我知道您可能已经阅读过类似的问题,但我的问题有点不同,我使用 python 3.8.3 并且我知道 python 基础知识,所以我想开始使用 GUI,我从 Tkinter 开始。我写了一个小程序,它什么都不做,我只是在尝试,但它不起作用,我不知道为什么。它给出了无法找到文件的错误,我尝试执行人们在其他问题中告诉的代码,但它不起作用,可能是由于 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()

所以当我运行它时,我得到这样的错误:

tkinter.TclError: couldn't open "images.jpg": no such file or directory

该文件位于保存 python 程序的同一文件夹中。

有任何想法吗?

标签: pythonuser-interfacetkinter

解决方案


你能显示你的工作目录吗?我认为这是目录的问题。如果它在同一级别的目录下,也许你可以试试这个:'/images.jpg'


推荐阅读