我主要是努力将图像导入到Tkinter for Python 3.5中,这是针对我的A2项目,并且每个将.JPG文件导入窗口的方法都会遇到困难。我下面的内容是我的另一个窗口的GUI层,基于我找到的另一个线程但根本不起作用。任何协助赞赏。
import tkinter
from tkinter import *
root=Tk()
root.geometry('1000x700')
root.resizable(False, False)
root.title("Skyrim: After Alduin")
photo = PhotoImage(file="Skyrim_Map")
map=Label(root, image=photo)
map.photo=photo
map.pack()
root.mainloop()
这是我收到的错误:
Traceback (most recent call last):
File "E:\Programming\Text_Adventure_Project\GUI_Interface-S_AA.py", line 14, in <module>
photo = PhotoImage(file="Skyrim_Map")
File "C:\Users\Harrison\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 3393, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Harrison\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 3349, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "Skyrim_Map": no such file or directory
首先检查路径,如果正确的路径仍然存在错误“没有这样的文件或目录”请编辑并放入最新版本的代码。如果错误是“无法识别图像文件中的数据”,则可以使用PIL进行更正
[...]
from PIL import ImageTk
[...]
photo = ImageTk.PhotoImage(file='Skyrim_Map.jpg')
[...]
另一个角度来看 :
课程申请:
...
self.photo=PhotoImage(file='picture.gif')
self.item=self.can.create_image(200, 100, image=self.photo)
...
希望能帮到你! ;)