【问题标题】:Python/tkinter: elapsed time in label/updating a label constantly?Python / tkinter:标签的经过时间/不断更新标签?
【发布时间】: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


    【解决方案1】:

    我们将让该函数告诉根 tkinter 实例 (app) 在一定时间后再次调用它,而不是使用循环更新它,使用 after() 方法。这样,它将以指定的频率无限期地运行。这类似于递归,但函数每次运行时都会完成,而不是等待递归调用结束,因此不会遇到递归限制。

    我们还将只创建一次Label,然后简单地重新配置它。

    改变这个:

    #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
    

    到这里:

    #Time readout for track
    
    nowPlaying = Label(app, text='') # create the Label
    nowPlaying.grid() # and grid it
    
    def ReadOut(widget, player): # take a widget and a Player
        widget.config(text=player.time) # reconfigure the widget with player.time
        app.update_idletasks() # I don't know if this is necessary -
        # try taking it out and see what happens
    
        # this will call this function again after 200ms,
        # passing it the same widget and player.
        # we use a lambda to more easily pass arguments to the function
        app.after(200, lambda: ReadOut(widget, player))
    
    ReadOut(nowPlaying, player) # call the function and pass it the Label and the Player
    app.mainloop() # keep at the end
    

    【讨论】:

    • 你能更详细地描述失败吗?
    猜你喜欢
    • 2015-05-24
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    相关资源
    最近更新 更多