【发布时间】:2020-10-21 09:01:06
【问题描述】:
我正在做一个使用 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)
为什么会这样?我应该如何修复代码以制作更好的音乐播放器?对此的任何建议将不胜感激!
【问题讨论】:
-
尝试:command=lambda: press(button_text.get())..) 并在你的函数中删除 word = button_text.get()。
-
删除
time.sleep(5)后会发生什么?这是休眠线程,并中断主事件循环。我猜这就是窗口没有正确更新的原因。 -
尝试将
from pygame import mixer更改为from pygame.mixer import *。 -
@Kingsley 没有 time.sleep(5),歌曲不会播放。使用 time.sleep(5),歌曲播放 5 秒....
-
我今天会尝试这些建议,谢谢您的意见!