AMM
AMM

Reputation: 196

Image not appearing in Ttkinter window

What I'm trying to do is to display the image cover from a flac file like in the pic below (this one is hard coded).

Text

I ripped the cover with the function getCoverFlac from my code, but the problem begins when I try to update this img with my imgSet fucntion, the image to load exist(can see it in the dir and can even be used hard coded), but the image wont appear in the Ttkinter window. I believe its receiving the right file name since it returns the name correctly:

name of cover to display:Shikao Suga - Yuudachi ('99 NHK Hall 0 Live).flacCover.jpg

Text

so how can I fix this? full code bellow:

import pygame
import tkinter as tkr
from tkinter.filedialog import askdirectory
from tkinter import *
import os
from mutagen.flac import FLAC, Picture
import os.path
from PIL import ImageTk, Image

music_player = tkr.Tk()
cont = 0
music_player.title("My Music Player")
music_player.geometry("600x600")
play_list = tkr.Listbox(music_player, font="Helvetica 12 bold", bg='yellow', selectmode=tkr.SINGLE)

pygame.init()
pygame.mixer.init()

def play():
    pygame.mixer.music.load(play_list.get(tkr.ACTIVE))
    #gets  and set the var name
    var.set(play_list.get(tkr.ACTIVE))
    #gets the cover and sets the the img that will be in panel
    getThecover = getCoverFlac(play_list.get(tkr.ACTIVE))
    imgSet(getThecover)
    pygame.mixer.music.play()
    
def stop():
    pygame.mixer.music.stop()
def pause():
    pygame.mixer.music.pause()
def unpause():
    pygame.mixer.music.unpause()
def selectDir():
    directory = askdirectory()
    os.chdir(directory)
    song_list = os.listdir()
    for item in song_list:
        pos = 0
        play_list.insert(pos, item)
        pos += 1

def  getCoverFlac(flac_file):
#this fucntion extracts the image from the flac file and saves it in the dir where the flac file is.
        flacObj = FLAC(flac_file)
        coverArt = flacObj.pictures
        coverName = str(flac_file)+"Cover.jpg"
        if os.path.isfile(coverName):
            print(coverName+" already exists")
        else:
            for img in coverArt:
                if img.type == 3:
                    with open(coverName, "wb") as f:
                        f.write(img.data)
                    print(coverName+" created and saved")
                    #img.show()
        return coverName
    
def imgSet(var):
#sets the global var "img" to var
    global img
    print("name of cover to display:"+var)
    resizeImg(var)
    img = ImageTk.PhotoImage(Image.open(var))

def resizeImg(imgName):
    basewidth = 300
    img = Image.open(imgName)
    wpercent = (basewidth/float(img.size[0]))
    hsize = int((float(img.size[1])*float(wpercent)))
    img = img.resize((basewidth,hsize), Image.ANTIALIAS)
    img.save(imgName)

Button1 = tkr.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="PLAY", command=play, bg="blue", fg="white")
Button2 = tkr.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="STOP", command=stop, bg="red", fg="white")
Button3 = tkr.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="PAUSE", command=pause, bg="purple", fg="white")
Button4 = tkr.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="UNPAUSE", command=unpause, bg="orange", fg="white")
Button5 = tkr.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="Music Dir", command=selectDir, bg="green", fg="white")



var = tkr.StringVar() 


song_title = tkr.Label(music_player, font="Helvetica 12 bold", textvariable=var)
resizeImg('Haddaway - What Is Love (70 Mix).flacCover.jpg')
img = ImageTk.PhotoImage(Image.open('Haddaway - What Is Love (70 Mix).flacCover.jpg'))

    
panel = tkr.Label(music_player, image = img, justify=CENTER)

song_title.pack()
panel.pack(fill="x")
Button1.pack(fill="x")
Button2.pack(fill="x")
Button3.pack(fill="x")
Button4.pack(fill="x")
Button5.pack(fill="x")
play_list.pack(fill="both", expand="yes")
music_player.mainloop()

Upvotes: 0

Views: 83

Answers (1)

acw1668
acw1668

Reputation: 46669

You just update the global variable img inside imageSet(), but forget to update the image of the label using panel.config(image=img):

def imgSet(var):
#sets the global var "img" to var
    global img
    print("name of cover to display:"+var)
    resizeImg(var)
    img = ImageTk.PhotoImage(Image.open(var))
    panel.config(image=img) # update image of label "panel"

Upvotes: 2

Related Questions