【问题标题】:Toggle pausing .sleep in python在 python 中切换暂停 .sleep
【发布时间】:2021-08-23 20:00:57
【问题描述】:

这是我目前在按下某些键后正在使用的提醒

import keyboard
import time
import threading
from playsound import playsound

def handle():
    time.sleep(30)
    print("text")
    playsound("file location.wav")

while True:
    if keyboard.read_key() == "f4":
        t = threading.Thread(target=handle)
        t.start()

但是我希望添加一个选项,可以暂停正在播放的文件的倒计时,然后在再次按下时恢复它

我对 python 不是很好,任何帮助都是非常感谢

【问题讨论】:

    标签: python sleep keypress pause


    【解决方案1】:

    你好,祝你有美好的一天。

    希望这会有所帮助(如果您坚持使用“线程”包!)

    import keyboard
    import time
    import threading
    from playsound import playsound
    
    i = 30
    stop_threads = True
    
    def countdown():
        global stop_threads
        global i
        while i:
            if not stop_threads:
                print('Sec ' + str(i))
                time.sleep(1)
                i -= 1
            else:
                break
        else:
            print('Alarm!')
            playsound('file.wav')
    
    
    while True:
        if keyboard.read_key() == "f4":
            t = threading.Thread(target=countdown)
            t.daemon = True
            stop_threads = False
            t.start()
        if keyboard.read_key() == "f3":
            stop_threads = True
            t.join()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多