0

当我将 tkinter 与 playsound 一起使用时,它会冻结,直到播放声音。我试过使用 block = False 但它根本不播放声音!

这是我的代码的一部分:

from tkinter import *
from tkinter import ttk
from playsound import playsound


class GameMaker:
    def __init__(self,root=None):
        self.sounds={}
    def AddSound(self,name,directory,overide=False,times=0,TimesOveride=0):
        if times != 0:
            name = name + str(times)
        if TimesOveride != 0:
            if times >= 10:
                return None
        else:
            if times >= TimesOveride:
                return None
            
        try:
            self.sounds[name]
            if overide == True:
                self.sounds[name]=directory
            else:
                AddSound(name,directory,times=times+1)
        except:
            self.sounds[name]=directory
    def PlaySound(self,sound):
        playsound(self.sounds[sound],block=False)
    def MapAll(self,command):
        keys = list("qwertyuiopasdfghjklzxcvbnm")
        for key in keys:
            self.Bind(key,command)
            print(key)
        

game.sounds['Tap-1'] = "C:\Users\Not Shownin\Desktop\GameMaker\v0.1\Tap-1.mp3"
game.sounds['Tap-2'] = "C:\Users\Not Shownin\Desktop\GameMaker\v0.1\Tap-2.mp3"


from random import randint
def Tap(event):
    print("tap")
    if randint(1,2) == 1:
        game.PlaySound("Tap-1")
    else:
        game.PlaySound("Tap-1")

我知道这很多,但还有更多。

我的代码的一个细分是它只是将目录添加到声音字典中,以便我以后可以播放它。

4

1 回答 1

5

我设法解决了我自己的问题!

from tkinter import *
from tkinter import ttk
from playsound import playsound
import threading


class GameMaker:
    def __init__(self,root=None):
        self.sounds={}
    def AddSound(self,name,directory,overide=False,times=0,TimesOveride=0):
        if times != 0:
            name = name + str(times)
        if TimesOveride != 0:
            if times >= 10:
                return None
        else:
            if times >= TimesOveride:
                return None
            
        try:
            self.sounds[name]
            if overide == True:
                self.sounds[name]=directory
            else:
                AddSound(name,directory,times=times+1)
        except:
            self.sounds[name]=directory
    def PlaySound(self,sound):
        play = threading.Thread(target=playsound, args=(self.sounds[sound],))
        play.start()
    def MapAll(self,command):
        keys = list("qwertyuiopasdfghjklzxcvbnm")
        for key in keys:
            self.Bind(key,command)
            print(key)
        

game.sounds['Tap-1'] = "C:\Users\Not Shownin\Desktop\GameMaker\v0.1\Tap-1.mp3"
game.sounds['Tap-2'] = "C:\Users\Not Shownin\Desktop\GameMaker\v0.1\Tap-2.mp3"


from random import randint
def Tap(event):
    print("tap")
    if randint(1,2) == 1:
        game.PlaySound("Tap-1")
    else:
        game.PlaySound("Tap-1")

我所做的唯一更改是:

  1. 我添加了一个新的导入
    import threading
    
  2. 我定义并启动了一个线程:
    play = threading.Thread(target=playsound, args=(self.sounds[sound],))
    play.start()
    
于 2022-02-26T18:15:42.067 回答