使用 playsound 模块时程序崩溃
Program is crashing while using playsound module
是的,我正在制作一个程序来播放我最喜欢的游戏的音乐。现在的问题是,当我调用 playsound
(module) 的 playsound
函数时,我的程序滞后并最终崩溃。这是我的代码:
import tkinter as tk
from playsound import playsound
import multiprocessing
class dynastyMusicer(tk.Tk):
def __init__(self, screenName=None, baseName=None, className='Tk',
useTk=True, sync=False, use=None):
super().__init__(screenName=screenName, baseName=baseName, className=className, useTk=useTk, sync=sync, use=use)
self.title("Dynasty Musicer")
self.geometry("250x100")
self.config(background="white")
chooseL = tk.Label(self, text="Which music to play?", background="white")
chooseL.place(x=50, y=0)
ops = ["Dynasty Dojo", "Dynasty Dojo Fight"]
self.stvar = tk.StringVar(self)
self.stvar.set("Music")
self.option = tk.OptionMenu(self, self.stvar, *ops)
self.option.place(y=30)
btn = tk.Button(self, text="Play", width=10, command=self.play)
btn.place(y=70)
stop = tk.Button(self, text="Stop", width=10, command=self.stop)
stop.place(x=160,y=70)
def stop(self):
multiprocessing.Process(target=playsound, args=(self.stvar.get()+".mp3"))
def play(self):
if self.stvar.get() == "Dynasty Dojo":
playsound(r"path")
elif self.stvar.get() == "Dynasty Dojo Fight":
playsound(r"path")
dm = dynastyMusicer()
dm.mainloop()
也正因为如此,我无法测试我的 stop
功能是否正常工作,因为当您单击播放按钮时,是的,您猜对了,它会崩溃。如果你想下载我使用的两个音乐文件,这里是链接:
您的代码有很多值得关注的地方。与:
tkinter
是单线程的,运行从单独的线程中使用它会导致问题,但如果可以确保 tk
对象不会进入另一个 processes/thread。
然而,在这种情况下,这种痛苦并不是不必要的,在这里你不需要使用任何东西,因为 playsound
可以 运行 而不会阻塞线程,例如: playsound(..., block=False)
我还注意到这里有一个停止按钮,您无法使用 playsound
停止启动的声音,所以现在您必须开始寻找其他线程声音库,而 pygame
是我能找到的最好的。虽然它有一些文件支持问题,但在大多数情况下应该没问题。
这是使用 pygame
并修改当前设置的更好代码:
import tkinter as tk
import pygame # pip install pygame
class DynastyMusicer(tk.Tk): # Using Pascal Case notation for class names
def __init__(self, *args, **kwargs): # Shorten all the optional arguments
super().__init__(*args, **kwargs) # or tk.Tk.__init__(....)
self.title("Dynasty Musicer")
self.geometry("250x100")
self.config(background="white")
pygame.init() # Initialize pygame
chooseL = tk.Label(self, text="Which music to play?", background="white")
chooseL.place(x=50, y=0)
self.ops = ["Dynasty Dojo", "Dynasty Dojo Fight"]
self.stvar = tk.StringVar(self)
self.stvar.set("Music")
self.option = tk.OptionMenu(self, self.stvar, *self.ops)
self.option.place(y=30)
btn = tk.Button(self, text="Play", width=10, command=self.play)
btn.place(y=70)
stop = tk.Button(self, text="Stop", width=10, command=self.stop)
stop.place(x=160,y=70)
def stop(self):
pygame.mixer.music.stop() # Stop the music being played
def play(self):
if self.stvar.get() != 'Music': # If not the default value, then
pygame.mixer.music.load(rf"{self.stvar.get()}.mp3") # Load the song
pygame.mixer.music.play() # Play the song
dm = DynastyMusicer()
dm.mainloop()
在这里,您的 if
语句被修改为缩短,因为您遵循 whitelisting 禁用任何 无效 输入([=43 除外) =]).
是的,我正在制作一个程序来播放我最喜欢的游戏的音乐。现在的问题是,当我调用 playsound
(module) 的 playsound
函数时,我的程序滞后并最终崩溃。这是我的代码:
import tkinter as tk
from playsound import playsound
import multiprocessing
class dynastyMusicer(tk.Tk):
def __init__(self, screenName=None, baseName=None, className='Tk',
useTk=True, sync=False, use=None):
super().__init__(screenName=screenName, baseName=baseName, className=className, useTk=useTk, sync=sync, use=use)
self.title("Dynasty Musicer")
self.geometry("250x100")
self.config(background="white")
chooseL = tk.Label(self, text="Which music to play?", background="white")
chooseL.place(x=50, y=0)
ops = ["Dynasty Dojo", "Dynasty Dojo Fight"]
self.stvar = tk.StringVar(self)
self.stvar.set("Music")
self.option = tk.OptionMenu(self, self.stvar, *ops)
self.option.place(y=30)
btn = tk.Button(self, text="Play", width=10, command=self.play)
btn.place(y=70)
stop = tk.Button(self, text="Stop", width=10, command=self.stop)
stop.place(x=160,y=70)
def stop(self):
multiprocessing.Process(target=playsound, args=(self.stvar.get()+".mp3"))
def play(self):
if self.stvar.get() == "Dynasty Dojo":
playsound(r"path")
elif self.stvar.get() == "Dynasty Dojo Fight":
playsound(r"path")
dm = dynastyMusicer()
dm.mainloop()
也正因为如此,我无法测试我的 stop
功能是否正常工作,因为当您单击播放按钮时,是的,您猜对了,它会崩溃。如果你想下载我使用的两个音乐文件,这里是链接:
您的代码有很多值得关注的地方。与:
tkinter
是单线程的,运行从单独的线程中使用它会导致问题,但如果可以确保tk
对象不会进入另一个 processes/thread。然而,在这种情况下,这种痛苦并不是不必要的,在这里你不需要使用任何东西,因为
playsound
可以 运行 而不会阻塞线程,例如:playsound(..., block=False)
我还注意到这里有一个停止按钮,您无法使用
playsound
停止启动的声音,所以现在您必须开始寻找其他线程声音库,而pygame
是我能找到的最好的。虽然它有一些文件支持问题,但在大多数情况下应该没问题。
这是使用 pygame
并修改当前设置的更好代码:
import tkinter as tk
import pygame # pip install pygame
class DynastyMusicer(tk.Tk): # Using Pascal Case notation for class names
def __init__(self, *args, **kwargs): # Shorten all the optional arguments
super().__init__(*args, **kwargs) # or tk.Tk.__init__(....)
self.title("Dynasty Musicer")
self.geometry("250x100")
self.config(background="white")
pygame.init() # Initialize pygame
chooseL = tk.Label(self, text="Which music to play?", background="white")
chooseL.place(x=50, y=0)
self.ops = ["Dynasty Dojo", "Dynasty Dojo Fight"]
self.stvar = tk.StringVar(self)
self.stvar.set("Music")
self.option = tk.OptionMenu(self, self.stvar, *self.ops)
self.option.place(y=30)
btn = tk.Button(self, text="Play", width=10, command=self.play)
btn.place(y=70)
stop = tk.Button(self, text="Stop", width=10, command=self.stop)
stop.place(x=160,y=70)
def stop(self):
pygame.mixer.music.stop() # Stop the music being played
def play(self):
if self.stvar.get() != 'Music': # If not the default value, then
pygame.mixer.music.load(rf"{self.stvar.get()}.mp3") # Load the song
pygame.mixer.music.play() # Play the song
dm = DynastyMusicer()
dm.mainloop()
在这里,您的 if
语句被修改为缩短,因为您遵循 whitelisting 禁用任何 无效 输入([=43 除外) =]).