當我使用PIL加載圖像並將其顯示在Tkinter創建的窗口中時,我很想念一個非常基礎的級別。什麼我想要做的最簡單的形式是:如何與Tkinter一起使用PIL?
import Tkinter as TK
from PIL import Image, ImageTk
im = Image.open("C:\\tinycat.jpg")
tkIm = ImageTk.PhotoImage(im)
tkIm.pack()
TK.mainloop()
當我試圖運行上面的代碼,我得到如下:
RuntimeError: Too early to create image
Exception AttributeError: "PhotoImage instance has no attribute
'_PhotoImage__photo'" in <bound method PhotoImage.__del__ of
<PIL.ImageTk.PhotoImage instance at 0x00C00030>> ignored
我已確認該文件存在,並且可以在圖像編輯器中打開,也可以使用im.show()顯示。我錯過了什麼?
可能重複的[Python Tkinter錯誤,「太早創建圖像」](http://stackoverflow.com/questions/10236857/python-tkinter-error-too-early-to-create-image) – 2013-02-27 18:23:40