Gravity
Gravity

Reputation: 23

So, how is curSongJson not defined?

Why does it say that curSongJson is not defined if I'm defining it in the displaySongs function when I run the application? I had it working before by removing the displaySongs function and just using a while loop but I need the function for tkinter and updating the label.

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()
# imageLabel = tkinter.Label(window)
# imageLabel.pack()

def GrabSpotifyCurSong():
    return curSongJson['item']['name']
def GrabSpotifyCurArtist():
    return curSongJson['item']['artists'][0]['name']
def GrabCurrentSongImage():
    return curSongJson['item']['album']['images'][0]['url']
    
def displaySongs():
    try:
        curSong = requests.get(endpoint, headers=spotifyHeaders)
        curSongJson = curSong.json()
    except:
        print("Please start listening to a song")
        time.sleep(2)
    # with open('CurrentSong.jpg','wb+') as SongImage:
    # response = requests.get(GrabCurrentSongImage())
    # SongImage.write(response.content)
    currentSong = GrabSpotifyCurSong()
    currentArtist = GrabSpotifyCurArtist()
    # imageLabel['text'] = f'{currentArtist} - {currentSong}'
    print(f'{currentArtist} - {currentSong}')
    #     window.after(4000,displaySongs)

displaySongs()
# window.mainloop()

Upvotes: 1

Views: 33

Answers (1)

Yatharth Ranjan
Yatharth Ranjan

Reputation: 471

The curSongJson is a local variable so it's scope is limited to the function where it is defined. So, it will not be accessible outside the displaySongs() method.

You can pass the currSongJson to other functions as parameters where needed like -

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()
# imageLabel = tkinter.Label(window)
# imageLabel.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():
    try:
        curSong = requests.get(endpoint, headers=spotifyHeaders)
        curSongJson = curSong.json()
    except:
        print("Please start listening to a song")
        time.sleep(2)
    # with open('CurrentSong.jpg','wb+') as SongImage:
    # response = requests.get(GrabCurrentSongImage(curSongJson))
    # SongImage.write(response.content)
    currentSong = GrabSpotifyCurSong(curSongJson)
    currentArtist = GrabSpotifyCurArtist(curSongJson)
    # imageLabel['text'] = f'{currentArtist} - {currentSong}'
    print(f'{currentArtist} - {currentSong}')
    #     window.after(4000,displaySongs)

displaySongs()
# window.mainloop()

Upvotes: 1

Related Questions