【发布时间】:2015-04-27 04:21:08
【问题描述】:
所以我试图编写代码来显示音频文件的播放时间,通过不断循环和更新 pyglet 的经过时间方法中的标签文本。我可以得到时间出现,但它不会更新。想知道如何更新 GUI 上的标签以显示经过的时间?时间循环接近底部,我提供了所有代码以防万一。
from tkinter import *
from tkinter.filedialog import askopenfilename
import pyglet
import pyglet.media as media
from threading import Thread
from tkinter import colorchooser
#make player and it's methods global
global player
player = pyglet.media.Player();
app = Tk()
app.title("Music PYlayer")
app.geometry("600x200")
have_avbin = True
#opens file
def openFile():
global f
f = filedialog.askopenfilename(filetypes = (("Mp3 files", "*.mp3"),("Wav files", "*.wav"),("All files","*.*")))
def aColor():
mycolor = colorchooser.askcolor()
color_name = mycolor[1] # #to pick up the color name in HTML notation, i.e. the 2nd element of the tuple returned by the colorchooser
app.configure(background=color_name)
#Creates menu bar for opening MP3s, and closing the program
menu = Menu(app)
file = Menu(menu)
file.add_command(label='Open', command= openFile) # replace 'print' with the name of your open function
file.add_command(label='Background color', command = aColor)
file.add_command(label='Exit', command=app.destroy) # closes the tkinter window, ending the app
menu.add_cascade(label='File', menu=file)
app.config(menu=menu)
#Run each app library mainloop in different python thread to prevent freezing
def playMusic():
global player_thread
player_thread = Thread(target=real_playMusic)
player_thread.start()
def stopMusic():
global player_thread
player_thread = Thread(target=real_stopMusic)
player_thread.start()
#Play open file function attached to button
def real_playMusic():
src=pyglet.media.load(f, streaming=False)
global player
player = pyglet.media.Player();
player.queue(src)
player.play()
pyglet.app.run()
#Stop the music function
def real_stopMusic():
player.pause()
#Play button creation
btnPlay = Button(app, text ="Play", command = playMusic)
btnPlay.place(x=75,y=100)
#Pause button creation
btnPause = Button(app)
btnPause.configure(text = "Stop", command = stopMusic)
btnPause.place(x=475,y=100)
#Time readout for track
def ReadOut():
time2 = player.time
i=0
if i <= 0:
nowPlaying=Label(app, text=time2)
nowPlaying.grid()
app.update_idletasks()
ReadOut()
app.mainloop() # keep at the end
【问题讨论】:
标签: python loops audio tkinter label