背景信息,以便您理解我为什么问这个问题:
我使用的Python版本是3.7
我是学生
在我的班上,我们两个月前开始学习Python
这门课的开始是我第一次遇到Python
你可以认为我对这门语言几乎一无所知。
不管怎样
在一个使用tkintergui的项目中,我尝试将多个图像编码到一个帧中。但是,错误消息:
'_tkinter.tcl错误:无法打开“snake1.jpg”:没有这样的文件或目录
每当我尝试运行程序时都会出现,尽管我已经确保我计划使用的所有图像都与我的项目放在同一个目录中,并且在尝试调用文件时也确保没有输入错误和错误。你知道吗
我想我放的东西有问题,但根据我使用的参考资料,似乎没有错误。你知道吗
在目录中,文件位于文件夹中,如下所示:
C:\Users\[我的帐户名]\PycharmProjects\Practice Coding\GUI Practice
GUI实践包括: 练习\u GUI_游戏.py, 蛇1.jpg, 蛇2.jpg, 蛇3.jpg, 蛇4.jpg。你知道吗
请检查代码,告诉我我做错了什么。所有的答案将被注意到和赞赏。非常感谢。你知道吗
from tkinter import *
game = Tk()
game.wm_title("Snake Collection")
game.config(bg="#EB5E55")
left1 = Frame(game, width=500, height=1000)
left1.grid(row=0, column=0, padx=15, pady=15)
def bonuslvl():
bonusimg1 = PhotoImage(file='snake1.jpg')
Label(left1, image=bonusimg1).grid(row=0, column=0, padx=5, pady=5)
bonusimg2 = PhotoImage(file='snake2.jpg')
Label(left1, image=bonusimg2).grid(row=0, column=1, padx=5, pady=5)
bonusimg3 = PhotoImage(file='snake3.jpg')
Label(left1, image=bonusimg3).grid(row=1, column=0, padx=5, pady=5)
bonusimg4 = PhotoImage(file='snake4.jpg')
Label(left1, image=bonusimg4).grid(row=1, column=1, padx=5, pady=5)
bonuslvl()
game.mainloop()
将每个jpeg图像的名称更改为其他名称,然后将其恢复为原始名称后,我遇到了一个新错误:
File "C:\Users\[my account name]\PycharmProjects\Practice Coding\GUI Practice\Practice_GUI_Game.py", line 10, in bonuslvl
bonusimg1 = PhotoImage(file='snake1.jpg')
File "C:\Users\[my account name]\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\[my account name]\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3498, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "snake1.jpg"
Process finished with exit code 1
所以,tkinter似乎不再支持
.jpg
。你知道吗我用PIL解决了你的问题。你知道吗
要安装PIL,请运行
pip install Pillow
。你知道吗我设法使它在我的本地机器上工作:
相关问题 更多 >
编程相关推荐