当我用tkinter和播放的声音,它会被释放,直到声音被播放。我试过用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")我知道这有很多,但还有很多。
我的代码的一个分解是,它只是在语音词典中添加了二进项,这样我就可以稍后播放它了。
发布于 2022-02-26 18:15:42
我解决了我自己的问题!
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")我所做的唯一改变是:
https://stackoverflow.com/questions/71279049
复制相似问题