Vic*_*ota 7 python tkinter python-imaging-library
我一直在尝试使用PIL调整图像大小,然后使用Tkinter显示它们,但程序崩溃了,我已将问题隔离到下面的第二行:
image = Image.open("0.gif")
photo = ImageTk.PhotoImage(image)
Run Code Online (Sandbox Code Playgroud)
这是我的进口:
from Tkinter import *
from PIL import Image, ImageTk
Run Code Online (Sandbox Code Playgroud)
我已经读过Tk必须初始化,我在程序到达程序中的那些行之前在程序中执行此操作.所以我不知道它是什么.
我在eclipse上运行OSX和python 2.7解释器(使用PyDev).
更新:
eclipse上的错误信息说:
STACK: Stack after current is in use
Run Code Online (Sandbox Code Playgroud)
我在使用 tkinter 之前就见过这个错误。我认为这与旧版本的 tkinter 有关。我更新了我的 python 版本和 tkinter 版本,然后它就消失了。当您在不同的操作系统/计算机/平台/Python 版本上运行代码时,是否会发生此错误?您使用什么版本的 tkinter?一些谷歌搜索显示这两个页面在使用 tkinter 时描述了相同的错误......
http://osdir.com/ml/python.leo.general/2008-03/msg00060.html
http://fornax.phys.unm.edu/lwa/trac/ticket/3
我看不到你所有的代码,但我敢打赌你的代码不一定有什么问题。以下代码对我有用......
from Tkinter import *
from PIL import Image, ImageTk
# resize image with PIL
im = Image.open('path to gif')
resized_im = im.resize((400,400,),Image.ANTIALIAS)
# display image in tkinter window
window = Tk()
tk_im = ImageTk.PhotoImage(resized_im)
window.geometry('%dx%d' % (resized_im.size[0],resized_im.size[1]))
label_image = Label(window, image=tk_im)
label_image.place(x=0,y=0,width=resized_im.size[0],height=resized_im.size[1])
window.mainloop()
Run Code Online (Sandbox Code Playgroud)
使用....
ubuntu 10.04 64位
python 2.6.5
python-imaging-tk 1.1.7
python-tk 2.6.5(使用tkinter版本8.5.0)
python成像库(PIL)1.1.7
eclipse 3.7.1
pydev 2.5.0.2012050419
祝你好运!