0
当我尝试使用Pillow在tkinter中显示图像时出现奇怪的问题。
我想原本在默认Tkinter的方式,这适用于GIF工作得很好显示图片:图像处理与枕头(在tkinter)时奇怪的错误
import tkinter as tk
root = tk.Tk()
src = tk.PhotoImage(file = "C:\\Users\\Matt\\Desktop\\K8pnR.gif")
label = tk.Label(root, image = src)
label.pack()
(K8pnR只是一个随机的gif我上imgur找到)
这个伟大的工程,但唯一的问题是我想显示其他文件类型。这导致我枕头,因为我在Python 3.4中工作。我试图启动与显示相同的文件,但使用枕头:
import tkinter as tk
from PIL import Image
root = tk.Tk()
src = Image.open("C:\\Users\\Matt\\Desktop\\K8pnR.gif")
img = tk.PhotoImage(file = src)
label = tk.Label(image = img, master = root)
label.pack()
这导致了一个非常奇怪的和丑陋的没有这样的文件或目录错误:
Traceback (most recent call last):
File "C:\Users\Matt\Desktop\pil test.py", line 7, in <module>
img = tk.PhotoImage(file = src)
File "C:\Python34\lib\tkinter\__init__.py", line 3416, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python34\lib\tkinter\__init__.py", line 3372, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "<PIL.GifImagePlugin.GifImageFile image mode=P size=494x260 at 0x26A6CD0>": no such file or directory
我尝试了不同的文件,不同的文件类型,甚至重新安装Pillow,但我仍然得到错误。
有谁知道这里发生了什么?我错过了非常明显的事情吗?
编辑:
当我尝试suggested修复,我得到这个怪异的错误:
Traceback (most recent call last):
File "C:\Users\Matt\Desktop\pil test.py", line 6, in <module>
img = ImageTk.PhotoImage(file = src)
File "C:\Python34\lib\site-packages\PIL\ImageTk.py", line 84, in __init__
image = Image.open(kw["file"])
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2297, in open
prefix = fp.read(16)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 632, in __getattr__
raise AttributeError(name)
AttributeError: read
我得到另一个奇怪的错误...请检查我的编辑。 – Mattstir
对不起,延迟似乎是'file = src'。不要命名该参数或正确命名,即'image = src'。 –
啊......谢谢!我知道这可能是我错过的一件小事......我把我的头发撕掉了。你太棒了! – Mattstir