0

Python 3.8
操作系统视窗 10

我正在尝试使用 GUI 创建警报。基本上,当满足条件(当前时间 == 设置时间)时,必须弹出一个带有两个按钮的小部件:取消和运行其他代码。如果我按下任何按钮,警报应该停止发出任何声音,因为两者都承认警报已响起。

我正在显示小部件并使用多处理来播放闹钟音乐,但我无法让闹钟停止。这是一个测试代码,它只是播放警报并要求用户按 Enter 键来停止它。如果我能完成这项工作,我将能够完成我的小部件警报

import multiprocessing
from playsound import playsound

def Child_process():
   print("Playing music")
   playsound('Perfect_Ring_Tone.mp3', block=False)

def terminator(process):
    process.terminate()
    
if __name__ == '__main__':
    # Create widget......

    # Play alarm sound
    P = multiprocessing.Process(target = Child_process)
    P.start()
    P.join()
    input("Press Enter to acknowledge the alarm")
    terminator(P)
    print("Child Process successfully terminated")
    # Do some more code...

如果我这样做,除非我删除警报,否则警报永远不会发出声音,block但是我必须等待它完成,这会破坏目的

4

1 回答 1

0

The problem is that you've used join() function which waits for the child process to die. This means that the main program stops until the child process have been terminated and the input instruction will never been executed because the function would never stop.

So simply remove P.join()

于 2020-12-22T18:51:29.410 回答