首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我使用PlaySound时Python会冻结

当我使用PlaySound时Python会冻结
EN

Stack Overflow用户
提问于 2022-02-26 17:54:32
回答 1查看 195关注 0票数 0

当我用tkinter和播放的声音,它会被释放,直到声音被播放。我试过用block = False,但是它根本不播放声音!

这是我代码的一部分:

代码语言:javascript
复制
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")

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

我的代码的一个分解是,它只是在语音词典中添加了二进项,这样我就可以稍后播放它了。

EN

回答 1

Stack Overflow用户

发布于 2022-02-26 18:15:42

我解决了我自己的问题!

代码语言:javascript
复制
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. I添加了一个新的导入线程

  1. 和我定义并启动了一个线程: play = threading.Thread(target=playsound,args=(self.soundssound,)) play.start()
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71279049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档