Home>

My code doesn't see the Label widget

line 67, in getPos
    TimePL.configure(text=pos)
NameError: name 'TimePL' is not defined

Code:

import random
from tkinter import *
from tkinter import filedialog
from pygame import mixer
from threading import Thread
import PyQt5
mixer.init()
background_name= "background.gif"
class MusicPlayer:
    def __init__(self, window ):
        r= random randint(0,100)
        if(r== 33):
            window_name= "Chocolate StarFish and HotDog flavored Water Player"
        elif(r== 66):
            window_name= "Chocolate StarFish player"
        else:
            window_name= "StarFish Player"
        window.geometry('1000x665'); window.title(window_name); window.resizable(0,0)
        img= PhotoImage(file=background_name)
        label= Label(root, image=img)
        label.image_ref= img
        label.pack()
        Load= Button(window, text= 'Load', width= 10, font= ('Times', 10), command= self.load)
        Play= Button(window, text= 'Play', width= 10,font= ('Times', 10), command= self.play)
        Pause= Button(window,text= 'Pause', width= 10, font= ('Times', 10), command= self.pause)
        Stop= Button(window ,text= 'Stop', width= 10, font= ('Times', 10), command= self.stop)
        Volm= Button(window, text= "Volume -", width= 10, font= ("Times", 10), command= self.Volm)
        Volp= Button(window, text= "Volume +", width= 10, font= ("Times", 10), command= self.Volp)
        TimePL= Label(text="", font=("Times",10))
        TimeAll= Label(text="TimeAL", font=("Times", 10))
        AudioName= Label(text="AudioName", font=("Times", 10))
        mixer.music.set_volume(1)
        Load.place(x=0,y=20);Play.place(x=110,y=20);Pause.place(x=220,y=20);Stop.place(x=110,y=60 ); Volm.place(x=0, y=60); Volp.place(x=220,y=60); TimePL.place(x=480, y=500)
        self.music_file= False
        self.playing_state= False
    defload(self):
        self.music_file= filedialog.askopenfilename()
    defplay(self):
        if self.music_file:
            mixer.init()
            mixer.music.load(self.music_file)
            mixer.music.play()
        TimePL.get()
    def pause(self):
        if not self.playing_state:
            mixer.music.pause()
            self.playing_state=True
        else:
            mixer.music.unpause()
            self.playing_state= False
    def stop(self):
        mixer.music.stop()
        self.music_file= False
    def Volp(self):
        volume= mixer.music.get_volume()
        volume= volume * 100 + 5
        if(volume<= 100):
            mixer.music.set_volume(volume/100)
    defVolm(self):
        volume= mixer.music.get_volume()
        volume= volume * 100 -5
        if(volume >= 0):
            mixer.music.set_volume(volume/100)
    def getPos(self):
        pos= mixer.music.get_pos()/1000
        print("Time played: "+str(pos))
        TimePL.configure(text=pos)
    def repeated(self):
        self.getPos(self)
root= Tk()
app=MusicPlayer(root)
def repeated():
    MusicPlayer.repeated(MusicPlayer)
    root.after(1000, repeated)
root.after(0, repeated())
root.mainloop()
  • Answer # 1

    I didn't check your application logic, just fixed some bugs that I came across.

    You must make an objectTimePLclass attribute self.TimePL, then you will have access to it in any class method.

    import random
    from tkinter import *
    from tkinter import filedialog
    from pygame import mixer
    # ??? from threading import Thread
    # ??? import PyQt5
    mixer.init()
    background_name= "gif.gif" # set your image
    class MusicPlayer:
        def __init__(self, window):
            super().__init__()
            r= random randint(0, 100)
            if(r== 33):
                window_name= "Chocolate StarFish and HotDog flavored Water Player"
            elif(r== 66):
                window_name= "Chocolate StarFish player"
            else:
                window_name= "StarFish Player"
            window.geometry('1000x665');
            window.title(window_name); window.resizable(0,0)
            img= PhotoImage(file=background_name)
            label= Label(root, image=img)
            label.image_ref= img
            label.pack()
            Load= Button(window, text= 'Load', width= 10, font= ('Times', 10), command= self.load)
            Play= Button(window, text= 'Play', width= 10,font= ('Times', 10), command= self.play)
            Pause= Button(window,text= 'Pause', width= 10, font= ('Times', 10), command= self.pause)
            Stop= Button(window ,text= 'Stop', width= 10, font= ('Times', 10), command= self.stop)
            Volm= Button(window, text= "Volume -", width= 10, font= ("Times", 10), command= self.Volm)
            Volp= Button(window, text= "Volume +", width= 10, font= ("Times", 10), command= self.Volp)
    # TimePL= Label(text="", font=("Times",10))
    # +++ vvvv
            self.TimePL= Label(text="", font=("Times",10)) # +++ self
            TimeAll= Label(text="TimeAL", font=("Times", 10))
            AudioName= Label(text="AudioName", font=("Times", 10))
            mixer.music.set_volume(1)
            Load.place(x=0,y=20);
            Play.place(x=110,y=20);
            Pause.place(x=220,y=20);
            Stop.place(x=110,y=60);
            Volm.place(x=0, y=60);
            Volp.place(x=220,y=60);
            self.TimePL.place(x=480, y=500) # +++ self
            self.music_file= False
            self.playing_state= False
        defload(self):
            self.music_file= filedialog.askopenfilename()
        defplay(self):
            if self.music_file:
                mixer.init()
                mixer.music.load(self.music_file)
                mixer.music.play()
    # ??? self.TimePL.get()
        def pause(self):
            if not self.playing_state:
                mixer.music.pause()
                self.playing_state=True
            else:
                mixer.music.unpause()
                self.playing_state= False
        def stop(self):
            mixer.music.stop()
            self.music_file= False
        def Volp(self):
            volume= mixer.music.get_volume()
            volume= volume * 100 + 5
            if(volume<= 100):
                mixer.music.set_volume(volume/100)
        defVolm(self):
            volume= mixer.music.get_volume()
            volume= volume * 100 -5
            if(volume >= 0):
                mixer.music.set_volume(volume/100)
        def getPos(self):
            pos= mixer.music.get_pos()/1000
            print("Time played: "+str(pos))
            self.TimePL.configure(text=pos) # +++ self
        def repeated(self):
    #self.getPos(self)
            self.getPos() # +++
    root= Tk()
    app=MusicPlayer(root)
    def repeated():
    # ??? MusicPlayer.repeated(MusicPlayer)
        app.repeated() # +++
        root.after(1000, repeated)
    root.after(0, repeated())
    root.mainloop()
    

    thanks, but Threading and pyqt5 are left as garbage) I forgot to remove

    3hhr2022-02-14 07:08:21