PIL Image.open()变量输入AttributeError

时间:2018-03-30 08:24:34

标签: python tkinter jpeg python-imaging-library tkinter-canvas

我正在尝试使用filepath作为Entry对象在Tkinter上显示图像。有必要使用PIL,因为它是一个.jpg文件。但即使我能够从tk.Entry中检索文件路径作为字符串,我也无法将其显示在Tkinter Canvas上。以下是有关该问题的一些重要代码行。

path = StringVar()
e1 = Entry(window, width=60, textvariable=path)
e1.pack()

canvas = Canvas(window, width=400, height=300)
canvas.pack()

img = ImageTk.PhotoImage(Image.open(path.get()))
canvas.create_image(20, 20, anchor=NW, image=img)

我得到的错误是:

    Traceback (most recent call last):
  File ".\GUI.py", line 60, in <module>
    img = ImageTk.PhotoImage(Image.open(path.get()))
  File "C:\Program Files\Python36\lib\site-packages\PIL\Image.py", line 2549, in open
    fp = io.BytesIO(fp.read())
AttributeError: 'str' object has no attribute 'read'

我该如何解决这个问题。任何帮助,将不胜感激。谢谢

0 个答案:

没有答案
相关问题