好吧,我彻底糊涂了。所以我最近开始使用 Steam API,我决定开始做一些简单的事情,显示个人资料的头像图像。
事情是,程序运行没有错误,只是它不显示图像。
这是显示图像的代码:
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)
这是其余的代码:
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()
我相信这是非常简单的事情,但我无法弄清楚是什么导致图像不显示。