cha*_*arg 0 tkinter python-3.9 python-playsound
当我将 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")
Run Code Online (Sandbox Code Playgroud)
我知道很多,但还有更多。
我的代码的一个细分是,它只是将目录添加到声音字典中,以便我稍后可以播放它。
我设法解决了我自己的问题!
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")
Run Code Online (Sandbox Code Playgroud)
我所做的唯一改变是:
import threading
Run Code Online (Sandbox Code Playgroud)
play = threading.Thread(target=playsound, args=(self.sounds[sound],))
play.start()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
572 次 |
| 最近记录: |