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
Related questions
- python : How to create a large text input field?
- python : How to keep the scale of buttons, text, etc in tkinter?
- python : how to find out the size of a Tkinter window?
- python : Opening photo for background doesn't work
- python : How to create a window in the game for example (dota2)
- python : Error with matrix multiplication. Numpy
- Can't uninstall/install Python
- python : Why does list.remove() incorrectly remove elements in a loop?
- Loading bar in Python 3.x
I didn't check your application logic, just fixed some bugs that I came across.
You must make an object
TimePL
class attributeself.TimePL
, then you will have access to it in any class method.thanks, but Threading and pyqt5 are left as garbage) I forgot to remove
3hhr2022-02-14 07:08:21