简单模拟了定时器功能,需要的朋友可以自己改写和扩充功能。
import datetime
import winsound
import time
import randomdef Timer(y, m, d, h, mu, s):
'''参数分别为年、月、日、时、分、秒'''
stopTime = datetime.datetime(y, m, d, h, mu, s)
maxTime = stopTime + datetime.timedelta(minutes=1)
while True:
now = datetime.datetime.now()
if now >= stopTime:
if now > maxTime:
print('时间已过1分钟,请重新设置时间')
break
else:
#每次闹铃声音频率不同
freq = random.randint(400, 1000)
winsound.Beep(freq, 2000)
time.sleep(2)
else:
time.sleep(1)
delta = stopTime - now
print('剩余:', delta.seconds, '秒')Timer(2017, 2, 8, 20, 31, 0)
标签:定时器,Python,datetime,简易,stopTime,time,print,import,now
From: https://blog.51cto.com/u_9653244/6451093