用Python中的DateTime模块来创建闹钟,
用Python中的playsound库来播放闹钟声音。
代码
from datetime import datetime #使用Python中的DateTime模块来创建闹钟
from playsound import playsound #并用Python中的playsound库来播放闹钟声音
# 输入
alarm_time = input("请输入闹钟时间, 示例: 09:50:00 am\n")
# 时
alarm_hour = alarm_time[0:2]
# 分
alarm_minute = alarm_time[3:5]
# 秒
alarm_seconds = alarm_time[6:8]
# 上午或下午
alarm_period = alarm_time[9:11].upper() #upper把字符串转换成大写
print("完成闹钟设置..")
while True:
now = datetime.now()
current_hour = now.strftime("%I")
current_minute = now.strftime("%M")
current_seconds = now.strftime("%S")
current_period = now.strftime("%p")
# 时间判断
if alarm_period == current_period:
if alarm_hour == current_hour:
if alarm_minute == current_minute:
if alarm_seconds == current_seconds:
print("起来啦!")
# 闹钟铃声
playsound('C:/学习/闹铃.mp3') #这里要填入你电脑中的bgm的地址,必须得是mp3(因为playsound只支持mp3).
break
效果