Python, tkinter: Why is this jpeg not showing?

Trying to display images from the Internet in my GUI.

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 that I get a blue square in which the image should be. How to fix it?

Using python 3.3 for windows

0
source share
1 answer

Now this is a wild hunch, but I just remembered a similar problem. Thus, I was able to reproduce your "blue box", so this may be your problem. I will just try it.

I assume that PhotoImage is being created in some other area (maybe the showImage(self, id) method or something like that) and no reference to it goes beyond the scope of this method. This causes PhotoImage to collect garbage at the end of this method, even if it is used in a shortcut!

Try to create a variable that exists throughout the life of the frame and bind PhotoImage to this variable (for example, self.images[ID] when using the class for the GUI or some other global variable otherwise). If I'm right, and this is really a problem, then this should help.

+1
source

Source: https://habr.com/ru/post/1492456/


All Articles