Python,tkinter:为什么此jpeg不显示? [英] Python, tkinter: Why is this jpeg not showing?

查看:101
本文介绍了Python,tkinter:为什么此jpeg不显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图在我的GUI窗口中显示来自互联网的图片.

Trying to display a picture from the internet on my GUI window.

到目前为止,我的代码是:

So far my code is:

picURL = "https://graph.facebook.com/" + ID + "/picture"
picBytes= urlopen(picURL).read()
picData = io.BytesIO(picBytes)
picPil = Image.open(picData)
picTk = ImageTk.PhotoImage(picPil)
label = Label(image = picTK, bg = "blue").pack()

问题是我得到的只是一个应该显示图片的蓝色框.我该如何解决?

The problem is all I get is a blue box where the picture should be. How do I fix this?

在Windows上使用python 3.3

Using python 3.3 on windows

推荐答案

现在这是一个疯狂的猜测,但是我只记得一个类似问题.我能够以这种方式重现您的蓝框",所以这也可能是您的问题.我试试看.

This is a wild guess now, but I just remembered a similar problem. I was able to reproduce your "blue box" this way, so this might be your problem, too. I'll just give it a try.

我假设PhotoImage是在其他范围内创建的(可能是方法showImage(self, id)或类似的方法),并且对该图像的引用都没有超出此方法的范围.这样的效果是,PhotoImage将在此方法结束时被垃圾回收,即使 在Label中使用它也是如此!

I assume that the PhotoImage is created in some other scope (maybe a method showImage(self, id) or something like that) and no reference to it is kept beyond the scope of this method. This has the effect that the PhotoImage will be garbage-collected at the end of this method, even though it is used in the Label!

尝试创建一个在帧的整个生命周期中都存在的变量,并将PhotoImage绑定到该变量(例如,对于GUI使用类时为self.images[ID],否则为某些全局变量).如果我是对的,而这确实是问题所在,那么这应该会有所帮助.

Try creating a variable that exists for the whole life time of the frame and bind the PhotoImage to that variable (e.g. self.images[ID] when using a class for the GUI, or some global variable otherwise). If I am right, and this is indeed the problem, then this should help.

这篇关于Python,tkinter:为什么此jpeg不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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