【发布时间】:2014-05-29 09:23:55
【问题描述】:
我正在使用 Tkinter 在 python 上编写一个小游戏(顺便说一下,我不允许使用任何其他非内置模块)并且我想在主窗口上播放背景歌曲,这就是包含标题,以及转到其他窗口和内容的按钮...
所以问题是当我进入另一个窗口时我需要停止声音,但是当我按下按钮转到另一个窗口时,歌曲会继续播放......
我正在使用 winsound 模块,并定义了几个函数(顺便说一句,这太邪恶了)当我第一次使用线程运行程序时播放歌曲...
这就是交易,我想在函数“killsound”中添加一些东西,这样我就可以将它添加到每个按钮,然后当我按下任何按钮打开任何其他窗口时,声音就会被杀死。
我希望像“a.kill()”或“a.stop()”这样的东西,但它没有用。
而且我真的不知道如何在 winsound 上使用 SND_PURGE ...虽然我知道 SND_PURGE 不再适用于新的 Windows 操作系统(获得 Win8.1)
你能帮帮我吗?
谢谢! (对不起,令人毛骨悚然的英语......)
def Play(nombre): #This function Is the core of the winsound function
ruta = os.path.join('Audio',nombre)
Reproducir= winsound.PlaySound(ruta,winsound.SND_FILENAME)
return Reproducir
def PlayThis():
while flag_play:
try:
return Play('prettiest weed.wav')
except:
return "Error"
def PlayThisThread():
global flag_play
flag_play= True
a=Thread(target=PlayThis, args=())
a.daemon = True
a.start()
PlayThisThread()
def killsound(): #This is the function I want, for killing sound.
global flag_play
flag_play = False
【问题讨论】:
标签: python multithreading audio tkinter