Python,写一个非常基本的闹钟

时间:2014-07-02 12:25:16

标签: python time alarm beep

我刚刚在python中写了一个非常基本的闹钟,当我在下午2:15准备了一些食物时警告我,但是我已经读到了某个时间,当其他程序正在处理时,睡眠时间缩短了,我该怎么办?用而代替? 我目前的代码,它非常基本 -

import time
import winsound
time.sleep(370800)
winsound.Beep(1000,10000)

1 个答案:

答案 0 :(得分:1)

定期检查。下面的代码应该没问题。您可以根据需要调整timeout变量:

timeout = 10
target = time.time() + 370800

while True:
    diff = target - time.time()
    if diff < timeout:
        if diff > 0:  # highly unlikely that diff will go below 0 but just in case
            time.sleep(diff)
        break
    else:
        time.sleep(timeout)

winsound.Beep(1000,10000)