ImageTk.PhotoImage崩溃 [英] ImageTk.PhotoImage Crash

查看:357
本文介绍了ImageTk.PhotoImage崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用PIL调整图像大小,然后使用Tkinter显示它们,但是程序崩溃了,我将问题隔离到下面的第二行:

I have been trying to resize images using PIL then display them using Tkinter, but the program has been crashing and I've isolated the problem to the second line below:

image = Image.open("0.gif")
photo = ImageTk.PhotoImage(image)

这是我的进口货

from Tkinter import * 
from PIL import Image, ImageTk 

我已经读到必须先初始化Tk,然后在程序到达那些行之前在程序中进行此操作.所以我不知道那是什么.

I've read around that Tk must be initialized and I do this in the program before it reaches those lines in the program. So I don't know what it is.

我在eclipse上运行OSX和python 2.7解释器(使用PyDev).

I am running OSX and python 2.7 interpreter on eclipse (using PyDev).

更新:

eclipse上的错误消息显示:

Error message on eclipse says:

STACK: Stack after current is in use

推荐答案

我一直在为当前项目同时使用Tk,PIL和调整图像大小,以下代码对我来说很好用.

I've been using both Tk, PIL and resizing images for a current project and the following code works fine for me.

#Imports
from Tkinter import * 
from PIL import Image, ImageTk 

#Create Tk instance
root = Tk()

#Open image and resize
image = Image.open("path/to/image/file").resize((400,400), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)

在那之后,我发现像这样在tkinter中将图像最简单地显示为标签.

After that, I find it easiest to display the images as labels in tkinter like so.

image_label = Label(root, width = 400, height = 400, image = photo bd = 0)

(我喜欢bd = 0,否则图像周围会出现白色细边框.) 希望这对您有所帮助.祝你好运! 埃德

(I like the bd = 0 as otherwise I get a thin white border around the image.) Hope this has helped you. Good luck! Ed

这篇关于ImageTk.PhotoImage崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆