0

I'm working on Music player and when I play a new song I always call songInfoDisplay function. As you can see on the picture (link below) songs information are not displayed properly. Is there a way how to fix it? Thank you for your time and answers.

master = Tk()
def playSong():
        song = lb.get(ACTIVE)
        pathToSong = (path + "\\" + song)
        mixer.music.load(pathToSong)
        mixer.music.play()
        audio = MP3(pathToSong)
        songLength = int(audio.info.length)
        songInfo=mutagen.File(pathToSong, easy=True)     
        songInfoDisplay()

def songInfoDisplay():

    songLabel=Label(master, text=songInfo["title"])
    artistLabel=Label(master, text=songInfo["artist"])
    albumLabel=Label(master, text=songInfo["album"])

    artistLabel.grid(row=5, column=3, stick=E)
    songLabel.grid(row=6, column=3, stick=E)
    albumLabel.grid(row=7, column=3, stick=E)
Michal
  • 229
  • 3
  • 11
  • 1
    You should not create those labels inside `songInfoDisplay()` function. Create them once the program starts and update their text inside `songInfoDisplay()` instead. – acw1668 May 19 '20 at 06:12
  • Does this answer your question? [making-python-tkinter-label-widget-update](https://stackoverflow.com/questions/1918005) – stovfl May 19 '20 at 12:55
  • @acw1668 I'm not sure what you mean exactly. Could you post example? – Michal May 21 '20 at 09:11

1 Answers1

1

You should create those labels once when program starts, then update their text when a song is selected. Below is a modified example based on your code:

master = Tk()

# create the required labels
songLabel=Label(master)
artistLabel=Label(master)
albumLabel=Label(master)

artistLabel.grid(row=5, column=3, stick=E)
songLabel.grid(row=6, column=3, stick=E)
albumLabel.grid(row=7, column=3, stick=E)

def playSong():
        song = lb.get(ACTIVE)
        pathToSong = (path + "\\" + song)
        mixer.music.load(pathToSong)
        mixer.music.play()
        audio = MP3(pathToSong)
        songLength = int(audio.info.length)
        songInfo=mutagen.File(pathToSong, easy=True)     
        songInfoDisplay(songInfo) # pass songInfo to songInfoDisplay()

def songInfoDisplay(songInfo):
    # show the song information
    songLabel.config(text=songInfo["title"][0])
    artistLabel.config(text=songInfo["artist"][0])
    albumLabel.config(text=songInfo["album"][0])
acw1668
  • 40,144
  • 5
  • 22
  • 34