我正在做一个使用PyGame和Tkinter制作音乐播放器的项目。到目前为止,我已经建立了界面并让播放器播放两首不同的歌曲,但是我需要一些有关如何更好地使用PyGame混音器的建议。我的代码是:
from tkinter import *
import time, sys
from pygame import mixer
track_names = ['lady maria', 'cleric beast']
current_track = ''
def press(word):
global track_name
global current_track
word = button_text.get()
if word == 'PLAY':
update_button_text('PLAY')
for name in track_names:
window_2.delete(0, 'end')
current_track = name
window_2.configure(state='normal')
window_2.insert('end', name)
mixer.init()
mixer.music.set_volume(100)
mixer.music.load(f'C:/Users/user/Desktop/python/projects/etc/{name}.mp3')
mixer.music.play()
time.sleep(5)
if word == 'PAUSE':
update_button_text('PAUSE')
mixer.music.pause()
time.sleep(5)
if word == 'STOP':
mixer.music.stop()
time.sleep(5)
if word == 'NEXT':
pass
if word == 'PREVIOUS':
pass
def update_button_text(word):
if word == 'PLAY':
button_text.set('PAUSE')
elif word == 'PAUSE':
button_text.set('PLAY')
if __name__ == '__main__':
# create application window
app = Tk()
# title
app.title("Music Players")
# geometry
app.geometry('383x121')
# background color
app.configure(bg='orange')
equation = StringVar()
window_1 = Label(app, textvariable=equation)
window_1.grid(columnspan=4, ipadx=100, ipady=10)
equation.set('music player')
window_2 = Entry(app, width=30)
window_2.grid(columnspan=4, ipadx=100, ipady=10)
window_2.configure(state='disabled')
window_2.grid_columnconfigure((0, 1, 2), uniform="equal", weight=1)
# Create buttons
button_text = StringVar()
button_text.set("PLAY")
button1 = Button(app, textvariable=button_text, fg='yellow', bg='purple',
command=lambda: press(button_text), height=2, width=1)
button1.grid(row=2, column=0, sticky="NSEW")
button2 = Button(app, text='STOP', fg='yellow', bg='purple',
command=lambda: press('STOP'), height=2, width=1)
button2.grid(row=2, column=1, sticky="NSEW")
button3 = Button(app, text='NEXT', fg='yellow', bg='purple',
command=lambda: press('NEXT'), height=2, width=1)
button3.grid(row=2, column=2, sticky="NSEW")
button4 = Button(app, text='PREVIOUS', fg='yellow', bg='purple',
command=lambda: press('PREVIOUS'), height=2, width=1)
button4.grid(row=2, column=3, sticky="NSEW")
# start the GUI
app.mainloop()
因此,当我运行代码并单击 PLAY 时,播放器似乎已停止响应。尽管它会立即开始播放音乐,但不会显示标题。我有将歌曲的标题首先插入Tkinter条目的代码,然后是混音器函数,但是似乎总是先混音器函数。这是我遇到问题的部分(我认为):
def press(word):
global track_name
global current_track
word = button_text.get()
if word == 'PLAY':
update_button_text('PLAY')
for name in track_names:
window_2.delete(0, 'end')
current_track = name
window_2.configure(state='normal')
window_2.insert('end', name)
mixer.init()
mixer.music.set_volume(100)
mixer.music.load(f'C:/Users/user/Desktop/python/projects/etc/{name}.mp3')
mixer.music.play()
time.sleep(5)
为什么会这样?我应该如何修复代码以制作更好的音乐播放器?对此的任何建议将不胜感激!
答案 0 :(得分:1)
问题在于您的代码在遍历音轨时阻止了TKinter主事件循环。事件驱动程序无法阻止其事件,这就是为什么您看到窗口“锁定”的原因-无需重新绘制,调整大小,关闭等。控制这些内容的窗口代码永远不会收到消息,也永远无法对他们采取行动。
您的应用程序代码必须从不阻塞该循环超过一秒钟。
那么您该怎么做...使用计时器或线程完成应用程序的工作。计时器易于使用,它们只是在主循环中将函数调用添加到队列中。在 X 毫秒后,将调用该函数。
例如:
window = tk.Tk()
[...]
window.after( 1000, myFunction, arg1 )
将导致主事件循环在1000毫秒后调用您的myFunction( arg1 )
。然后在myFunction
内部,您可以再次将对myFunction
的调用排入队列,形成周期性循环。
PyGame混音器功能已经在单独的线程中播放音乐。因此,在您的情况下,除了轮询以查看音乐是否结束之外,没有任何实际必要。所有“ PLAY”代码所需要做的就是 start 播放下一个声音文件,然后返回。
在主循环中,我们可以创建一个新的计时器功能,以检查声音文件是否仍在播放,或者是否需要将下一个声音排队:
def checkPlaying( main_window ):
global current_track
if ( mixer.music.get_busy() == False ):
# Playing has finished
print("Sound finished, playing next" )
track_index, track_name = current_track
current_track = ( track_index + 1, '' ) # Advance to next track
press( 'PLAY' ) # Start playing
# Queue the next call to this function
main_window.after( 250, checkPlaying, main_window ) # call again later
由于它重新排队了一个定时器调用,因此需要在主函数中进行该初始调用:
# Start the Music Playing Check
app.after( 1000, checkPlaying, app )
只需修改press()
函数即可删除连续循环,而只播放当前的声音文件:
mixer.init()
current_track = ( 0, '' ) # track-index and name
def press(word):
global track_names
global current_track
word = button_text.get()
if word == 'PLAY':
update_button_text('PLAY')
track_index, track_name = current_track
if ( track_index >= len( track_names ) ): # if out of music, re-start
track_index = 0
# Start Playing the current track
name = track_names[ track_index ]
current_track = ( track_index, name )
mixer.music.set_volume(100)
mixer.music.load(f'C:/Users/user/Desktop/python/projects/etc/{name}.mp3')
mixer.music.play()
# Update the GUI
window_2.delete(0, 'end')
window_2.configure(state='normal')
window_2.insert('end', name)
就是这样。
参考代码:
from tkinter import *
import time, sys
from pygame import mixer
mixer.init()
track_names = [ 'car-horn2', 'car-horn', 'cash-register', 'dog-bark', 'duck-quack', 'rain-falling', 'single-ding', 'turkey-gobble' ]
current_track = ( 0, '' )
def press(word):
global track_names
global current_track
word = button_text.get()
if word == 'PLAY':
update_button_text('PLAY')
track_index, track_name = current_track
if ( track_index >= len( track_names ) ): # if out of music, re-start
track_index = 0
# Play the current track
name = track_names[ track_index ]
current_track = ( track_index, name )
mixer.music.set_volume(100)
#mixer.music.load(f'C:/Users/user/Desktop/python/projects/etc/{name}.mp3')
mixer.music.load(f'{name}.mp3')
mixer.music.play()
window_2.delete(0, 'end')
window_2.configure(state='normal')
window_2.insert('end', name)
if word == 'PAUSE':
update_button_text('PAUSE')
mixer.music.pause()
time.sleep(5)
if word == 'STOP':
mixer.music.stop()
time.sleep(5)
if word == 'NEXT':
pass
if word == 'PREVIOUS':
pass
def checkPlaying( main_window ):
global track_names
global current_track
result = False
if ( mixer.music.get_busy() == True ):
# Still playing
result = True
else:
# Playing has finished
# TODO: Change button states, whatever
print("Sound finished, playing next" )
track_index, track_name = current_track
current_track = ( track_index + 1, '' )
press( 'PLAY' ) # start next track
result = False
# Queue the next call to this function
main_window.after( 250, checkPlaying, main_window )
return result
def update_button_text(word):
if word == 'PLAY':
button_text.set('PAUSE')
elif word == 'PAUSE':
button_text.set('PLAY')
if __name__ == '__main__':
# create application window
app = Tk()
# title
app.title("Music Players")
# geometry
app.geometry('383x121')
# background color
app.configure(bg='orange')
equation = StringVar()
window_1 = Label(app, textvariable=equation)
window_1.grid(columnspan=4, ipadx=100, ipady=10)
equation.set('music player')
window_2 = Entry(app, width=30)
window_2.grid(columnspan=4, ipadx=100, ipady=10)
window_2.configure(state='disabled')
window_2.grid_columnconfigure((0, 1, 2), uniform="equal", weight=1)
# Create buttons
button_text = StringVar()
button_text.set("PLAY")
button1 = Button(app, textvariable=button_text, fg='yellow', bg='purple',
command=lambda: press(button_text), height=2, width=1)
button1.grid(row=2, column=0, sticky="NSEW")
button2 = Button(app, text='STOP', fg='yellow', bg='purple',
command=lambda: press('STOP'), height=2, width=1)
button2.grid(row=2, column=1, sticky="NSEW")
button3 = Button(app, text='NEXT', fg='yellow', bg='purple',
command=lambda: press('NEXT'), height=2, width=1)
button3.grid(row=2, column=2, sticky="NSEW")
button4 = Button(app, text='PREVIOUS', fg='yellow', bg='purple',
command=lambda: press('PREVIOUS'), height=2, width=1)
button4.grid(row=2, column=3, sticky="NSEW")
# Start the Music Playing Check
app.after( 1000, checkPlaying, app )
# start the GUI
app.mainloop()