本文介绍了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 在此方法结束时被垃圾收集,即使它在标签中使用!

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 绑定到该变量(例如 self.images[ID] 当使用 GUI 类时,或某些全局否则变量).如果我是对的,这确实是问题所在,那么这应该会有所帮助.

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 没有显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:18