【发布时间】:2021-06-05 02:45:43
【问题描述】:
我正在尝试从歌曲专辑中获取图像以显示在带有歌曲标题和艺术家的窗口中,但它什么也没做。我试过用
替换“imageLabel”"imageLabel = tkinter.Label(window,image=tkinter.PhotoImage(file="CurrentSong.jpg"))" 但还是不行。
import requests
import time
import tkinter
token = ''
endpoint = "https://api.spotify.com/v1/me/player/currently-playing"
spotifyHeaders = {'Authorization':'Bearer ' + token}
requestAmount = 1
window = tkinter.Tk(className="|CurrentSong Spotify Song|")
window.geometry('400x400')
canvas = tkinter.Canvas(window,height=1000,width=1000)
canvas.pack()
songLabel = tkinter.Label(window,bg='grey')
songLabel.pack()
def GrabSpotifyCurSong(curSongJson):
return curSongJson['item']['name']
def GrabSpotifyCurArtist(curSongJson):
return curSongJson['item']['artists'][0]['name']
def GrabCurrentSongImage(curSongJson):
return curSongJson['item']['album']['images'][0]['url']
def displaySongs():
while True:
try:
curSong = requests.get(endpoint, headers=spotifyHeaders)
curSongJson = curSong.json()
break
except:
print("Please start listening to a song")
time.sleep(2)
with open('CurrentSong.png','wb+') as SongImage:
response = requests.get(GrabCurrentSongImage(curSongJson))
SongImage.write(response.content)
currentSong = GrabSpotifyCurSong(curSongJson)
currentArtist = GrabSpotifyCurArtist(curSongJson)
img = tkinter.PhotoImage(file="CurrentSong.png")
imageLabel = tkinter.Label(window,image=img)
# songLabel['text'] = f'{currentArtist} - {currentSong}'
# songLabel.place(height=400,width=400)
print(f'{currentArtist} - {currentSong}')
window.after(2500,displaySongs)
displaySongs()
window.mainloop()
【问题讨论】:
-
“还是不行。”是什么意思
-
@CoolCloud So "imageLabel = tkinter.Label(window,image=tkinter.PhotoImage(file="CurrentSong.jpg"))" 给我一个错误,上面写着:“无法识别数据图像文件“CurrentSong.jpg””和代码中的另一个文件什么都不做
-
可以直接加载图片数据:
response = requests.get(GrabCurrentSongImage(curSongJson)),image = ImageTk.PhotoImage(data=response.content)。
标签: python python-3.x tkinter