Question

Alright, I am thoroughly confused. So I recently started to use the Steam API and I decided to start out something simple, displaying the avatar image of a profile.

Thing is, the program runs with no errors except that it does not display the image.

Here is the code where it shows the image:

def displayImage():
global window
global STEAM_USER

response = urllib2.urlopen('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' + STEAM_API_KEY + '&steamids=' + STEAM_USER + '&format=xml')
htmlSource = response.read()
soup = BeautifulSoup(htmlSource)
avatar = soup.find('avatarfull').getText()
print avatar

image_bytes = urllib2.urlopen(avatar).read()
data_stream = io.BytesIO(image_bytes)
pil_image = Image.open(data_stream)
tk_image = ImageTk.PhotoImage(pil_image)
label = Label(window, image=tk_image)
label.pack(padx=5, pady=5)

And here is the rest of the code:

import urllib2
from Tkinter import *
from PIL import Image, ImageTk
from bs4 import BeautifulSoup
import io

STEAM_API_KEY = 'XXXX'

global window

window = Tk()
window.title('Steam Avatar Viewer')
window.geometry("215x215")


def newUser():
    global window
    global entry


    entry = Entry(window)
    button = Button(window, text='Search', width=10, command=getUser)

    entry.pack()
    button.pack()
def getUser():
    global STEAM_USER
    global entry

    steamUser = entry.get()
    steamConverterURL = 'http://www.steamidconverter.com/' + steamUser
    steamIDURL = urllib2.urlopen(steamConverterURL)
    steamIDSource = steamIDURL.read()
    a = BeautifulSoup(steamIDSource)
    for hit in a.findAll(attrs={'id':'steamID64'}):
        STEAM_USER = hit.contents[0]
    print STEAM_USER

    displayImage()

def displayImage():
    global window
    global STEAM_USER

    response =    urllib2.urlopen('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' +     STEAM_API_KEY + '&steamids=' + STEAM_USER + '&format=xml')
    htmlSource = response.read()
    soup = BeautifulSoup(htmlSource)
    avatar = soup.find('avatarfull').getText()
    print avatar

    image_bytes = urllib2.urlopen(avatar).read()
    data_stream = io.BytesIO(image_bytes)
    pil_image = Image.open(data_stream)
    tk_image = ImageTk.PhotoImage(pil_image)
    label = Label(window, image=tk_image)
    label.pack(padx=5, pady=5)

newUser()
window.mainloop()

I believe it is something very simple but I can't figure out what's causing the image not to display.

Was it helpful?

Solution

When you add a PhotoImage or other Image object to a Tkinter widget, you must keep your own reference to the image object. If you don’t, the image won’t always show up. Here is essentially what I'm trying to say:

photo = PhotoImage(...)
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()

You can refer to this.

OTHER TIPS

As has already been said, you must ensure you keep a reference to the image around, or else it will be removed by Python's garbage collector. In addition to the above method, the way I first learnt to overcome the issue was to simply append the image to a list, such as:

photo_list=[]
photo = PhotoImage(...)
photo_list.append(photo)

I liked this method at the time because it was obvious what the code was doing ( i.e. storing the picture to prevent it's deletion).

Either way, you must simply ensure your picture is kept around! Good luck :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top