在python 2.7中将图像添加到Tkinter的文本小部件中

viv*_*vkv 1 python tkinter python-imaging-library

我是Tkinter的新手,我正在尝试将图像添加到Tkinter python 2.7中的文本小部件中.我在互联网上查找了一些资源,据我所知,这是代码,但它给出了错误 - "

File "anim.py", line 11, in <module>
    text.image_create(END,image=photoImg)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2976, in image_create
    *self._options(cnf, kw))
  TypeError: __str__ returned non-string (type file) 
Run Code Online (Sandbox Code Playgroud)

"

请告诉我哪里出错了.提前致谢 :)

from PIL import Image,ImageTk
from Tkinter import *
root = Tk()
root.geometry("900x900+100+100")
image1 = open('IMG_20160123_170503.jpg')
photoImg = PhotoImage(image1)
frame = Frame(root)
frame.pack()
text = Text(frame)
text.pack()
text.image_create(END,image=photoImg)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

Rob*_*ray 7

你犯了两个小错误.当你open的形象,image1你正在使用Python的内置的open关键字,而不是Image.open,PIL的方法Image类.PhotoImage当你想要引用PIL的ImageTk.PhotoImage类时,你也可以参考Tkinter 的类.这是你的固定代码:

from Tkinter import *
from PIL import Image,ImageTk
root = Tk()
root.geometry("900x900+100+100")
image1 = Image.open("IMG_20160123_170503.jpg")
photoImg = ImageTk.PhotoImage(image1)
frame = Frame(root)
frame.pack()
text = Text(frame)
text.pack()
text.image_create(END,image=photoImg)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)