【问题标题】:Why does this not display the current song image? [duplicate]为什么这不显示当前歌曲图像? [复制]
【发布时间】: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


【解决方案1】:

带有tkinter 的图像必须是PhotoImage 实例,这里只是图像位置的字符串,tkinter 不明白这一点。此外,tkinter.PhotoImage 不识别 JPEG 格式,因此您必须将其转换为 PNG 或使用 PIL.ImageTk.PhotoImage 才能使用 JPEG。

  • 也适用于 JPEG 和其他格式:

    首先是pip install Pillow,然后:

    import tkinter
    from PIL import Image, ImageTk
    
    ....
    img = ImageTk.PhotoImage(Image.open("CurrentSong.jpg"))
    imageLabel = tkinter.Label(window,image=img)
    

    在此处进一步添加,您还可以使用ImageTk.PhotoImage(file="CurrentSong.jpg"),但这将消除您想要调整图像大小或对图像进行一些过滤时可以获得的灵活性。如果没有,请使用它。

  • 对于 GIF、PGM、PPM 和 PNG:

    img = tkinter.PhotoImage(file="CurrentSong.png")
    imageLabel = tkinter.Label(window,image=img)
    

另外请注意,如果这些在函数内部,您必须保持对对象的引用,以免在函数运行完成后被gc 收集。

【讨论】:

  • 我复制了您放置的内容并运行了它(还删除了其他标签,以防万一他们不会检查图像),同样的事情发生在没有图像被放入窗口。
  • @Gravity 其他标签不会越过图像,你也得到任何错误?
  • 没有错误,我把图片转成png,照原样做,看看能不能用
  • 现在我收到与以前相同的错误“无法识别图像文件“CurrentSong.png”中的数据”
  • @Gravity 我认为不会,因为您的实现中有一些错误。使用更新的代码编辑您的问题。
猜你喜欢
  • 2018-06-11
  • 2012-10-26
  • 2011-01-22
  • 2015-05-23
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多