程序在使用playsound模块时崩溃

2024-05-14 01:13:09 发布

您现在位置:Python中文网/ 问答频道 /正文

是的,我正在制作一个程序,播放我最喜欢的游戏的音乐。现在的问题是,当我调用playsound(模块)的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函数是否工作,因为当你点击播放按钮时,是的,你猜到了,它会崩溃。如果你想下载我使用的两个音乐文件,以下是链接:

Dynasty Dojo

Dynasty dojo fight


Tags: importselfnonedefplacetkdojostop
2条回答

代码中有很多东西需要查看。与:

  • tkinter是单线程的,从单独的线程运行它会导致问题,但是如果可以确保tk对象不会进入其他进程/线程,则可以使用multiprocessing

  • 然而,这种痛苦在这种情况下并不是没有必要的,在这里您将不需要使用任何这种痛苦,因为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语句被修改为short,因为您遵循whitelisting,这将禁用任何无效的输入(除了“Music”)

试一试 从playsound导入*

或 导入播放声音

相关问题 更多 >