为了在家安心学习,我决定写一段代码播放上课的铃声。
之前中学时就希望有一个这样的软件,周末就可以高效学习了。
但是网上搜不到相关软件,当时也不会写代码,就用闹钟来代替了。
现在自己写一个试试。
原本我是用C++写的,但后来发现C++太麻烦了。
要用到其他的库,那些库也不好安装,总是有一大堆难以解决的报错,就换Python了。
没想到Python这么方便。
功能一、播放音频
安装playsound库即可运行
from playsound import playsound
playsound('1.wav')
功能二、获取系统时间
import time
def main():
print(time.localtime())
year = time.localtime().tm_year
month = time.localtime().tm_mon
day = time.localtime().tm_mday
hour = time.localtime().tm_hour
minute = time.localtime().tm_min
second = time.localtime().tm_sec
print(year, '年', month, '月', day, '日')
print(hour, '时', minute, '分', second, '秒')
main()
功能三、上课铃声的定时播放
from playsound import playsound
import time
def ring():
playsound('1.wav')
time.sleep(5)
playsound('1.wav')
time.sleep(60)
def timeoff(hour,minute):
if hour == time.localtime().tm_hour and minute == time.localtime().tm_min:
ring()
while 1:
timeoff(8,0)
timeoff(8,45)
timeoff(8,50)
timeoff(9,35)
timeoff(9,50)
timeoff(10,35)
timeoff(10,40)
timeoff(11,25)
timeoff(11,30)
timeoff(12,15)
timeoff(13,30)
timeoff(14,15)
timeoff(14,20)
timeoff(15,20)
timeoff(16,5)
timeoff(16,10)
timeoff(16,55)
timeoff(18,30)
timeoff(19,15)
timeoff(19,20)
timeoff(20,5)
timeoff(20,10)
timeoff(20,55)
注释:
原本连续循环播放一分钟,效果不理想,利用了sleep语句使代码休眠,可以巧妙地解决这个问题
但是仍然存在CPU占用率过高的问题
上课时间表
编译
编译后再使用,就很方便了
优化一 针对CPU占用率过高的优化
原代码用了一个while(1)的循环
每一帧都在刷新比对系统时间,这使得CPU占用率过高
要求精确到分钟,那么现在我们每60秒检查一次时间,就可大大降低CPU占用率
from playsound import playsound
import time
import sys
#上课状态
status=0
def ring():
global status
status=status+1
if status%2==0:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:下课")
else:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:上课")
playsound('1.wav')
time.sleep(5)
playsound('1.wav')
def timeoff(hour,minute):
if hour == time.localtime().tm_hour and minute == time.localtime().tm_min:
ring()
def timecheck():
if time.localtime().tm_hour>=8 and time.localtime().tm_hour<=21:
timeoff(8,0)
timeoff(8,45)
timeoff(8,50)
timeoff(9,35)
timeoff(9,50)
timeoff(10,35)
timeoff(10,40)
timeoff(11,25)
timeoff(11,30)
timeoff(12,15)
timeoff(13,30)
timeoff(14,15)
timeoff(14,20)
timeoff(15,20)
timeoff(16,5)
timeoff(16,10)
timeoff(16,55)
timeoff(18,30)
timeoff(19,15)
timeoff(19,20)
timeoff(20,5)
timeoff(20,55)
else:
sys.exit()
while 1:
timecheck()
time.sleep(60)#每60秒检查一次
优化二 改变时间表的数据类型
使用字典、元组存储打铃时间和上下课状态
使代码更符合使用逻辑、运行时的漏洞更少
动态注释版本
了解程序实时的运行过程
from playsound import playsound
import time
def ring():
#一次打铃,重复播放两次,中间间隔5秒
playsound('1.wav')
time.sleep(5)
playsound('1.wav')
def itemcheck(yz,sta):
print("该item的键是:",yz,"(一个元组)")
print("元组小时部分:",yz[0],",分钟部分:",yz[1])
if yz[0] == time.localtime().tm_hour and yz[1] == time.localtime().tm_min:
ring()
if sta==0:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:下课")
#休眠5分钟
print("休眠5分钟")
time.sleep(240)
else:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:上课")
#休眠45分钟
print("休眠45分钟")
time.sleep(2640)
def timecheck():
#建立一个字典存放打铃时间和对应的状态
dict1 = {(8,0):1,(8,45):0,(8,50):1,(9,35):0,
(9,50):1,(10,35):0,(10,40):1,(11,25):0,
(11,30):1,(12,15):0,(13,30):1,(14,15):0,
(14,20):1,(15,5):0,(15,20):0,(16,5):0,
(16,10):1,(16,55):0,(18,30):1,(19,15):0,
(19,20):1,(20,5):0,(20,10):1,(20,55):0}
print("打铃时间表:",dict1)
print("正在遍历时间表里的全部item:")
for k in dict1.keys() :
#k是字典的键,dict1[k]是它对应的值
print("这个键是",k ,"它的值是",dict1[k])
itemcheck(k,dict1[k])
while 1:
timecheck()
time.sleep(60)#每60秒检查一次
纯净版本
from playsound import playsound
import time
def ring():
#一次打铃,重复播放两次,中间间隔5秒
playsound('1.wav')
time.sleep(5)
playsound('1.wav')
def itemcheck(yz,sta):
if yz[0] == time.localtime().tm_hour and yz[1] == time.localtime().tm_min:
if sta==0:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:下课")
else:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:上课")
ring()
if sta==0:
#休眠5分钟
time.sleep(240)
else:
#休眠45分钟
time.sleep(2640)
def timecheck():
#建立一个字典存放打铃时间和对应的状态
dict1 = {(8,0):1,(8,45):0,(8,50):1,(9,35):0,
(9,50):1,(10,35):0,(10,40):1,(11,25):0,
(11,30):1,(12,15):0,(13,30):1,(14,15):0,
(14,20):1,(15,5):0,(15,20):0,(16,5):0,
(16,10):1,(16,55):0,(18,30):1,(19,15):0,
(19,20):1,(20,5):0,(20,10):1,(20,55):0}
for k in dict1.keys() :
#k是字典的键,dict1[k]是它对应的值
itemcheck(k,dict1[k])
while 1:
timecheck()
time.sleep(30)#每30秒检查一次
优化三、睡眠时间自适应
from playsound import playsound
import time
breakflag=0
def ring():
#一次打铃,重复播放两次,中间间隔5秒
playsound('1.wav')
time.sleep(5)
playsound('1.wav')
def itemcheck(yz,sta):
if yz[0] == time.localtime().tm_hour and yz[1] == time.localtime().tm_min:
if sta==0:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:下课")
else:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:上课")
ring()
elif yz[0] > time.localtime().tm_hour or ( yz[0] == time.localtime().tm_hour and yz[1] > time.localtime().tm_min ):
gaptime=(yz[0]-time.localtime().tm_hour-1)*60+yz[1]+(60-time.localtime().tm_min)
print("距离",yz[0],":",yz[1],"的打铃还有",gaptime,"分钟")
global breakflag
breakflag=1
print("休眠",gaptime-1,"分钟")
time.sleep(gaptime*60-60)
def timecheck():
#建立一个字典存放打铃时间和对应的状态
dict1 = {(8,0):1,(8,45):0,(8,50):1,(9,35):0,
(9,50):1,(10,35):0,(10,40):1,(11,25):0,
(11,30):1,(12,15):0,(13,30):1,(14,15):0,
(14,20):1,(15,5):0,(15,20):0,(16,5):0,
(16,10):1,(16,55):0,(18,30):1,(19,15):0,
(19,20):1,(20,5):0,(20,10):1,(20,55):0}
global breakflag
breakflag=0
for k in dict1.keys() :
#k是字典的键,dict1[k]是它对应的值
if breakflag ==0:
itemcheck(k,dict1[k])
else :
break
while 1:
timecheck()
time.sleep(5)#倒计时1分钟时,每5秒检查一次
效果
打开软件时,自适应休眠
倒计时一分钟时,休眠结束,每5秒刷新一次
时间到了,打铃
打铃结束,进入休眠
优化四 加入铃声状态的判断
from playsound import playsound
import time
breakflag=0
def ring():
#一次打铃,重复播放两次,中间间隔5秒
playsound('1.wav')
time.sleep(5)
playsound('1.wav')
def itemcheck(yz,sta):
if yz[0] == time.localtime().tm_hour and yz[1] == time.localtime().tm_min:
if sta==0:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:下课")
else:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:上课")
ring()
elif yz[0] > time.localtime().tm_hour or ( yz[0] == time.localtime().tm_hour and yz[1] > time.localtime().tm_min ):
gaptime=(yz[0]-time.localtime().tm_hour-1)*60+yz[1]+(60-time.localtime().tm_min)
if sta==0:
st="下课铃"
else:
st="上课铃"
print("距离",yz[0],":",yz[1],"的",st,"还有",gaptime,"分钟")
global breakflag
breakflag=1
print("休眠",gaptime-1,"分钟")
time.sleep(gaptime*60-60)
def timecheck():
#建立一个字典存放打铃时间和对应的状态
dict1 = {(8,0):1,(8,45):0,(8,50):1,(9,35):0,
(9,50):1,(10,35):0,(10,40):1,(11,25):0,
(11,30):1,(12,15):0,(13,30):1,(14,15):0,
(14,20):1,(15,5):0,(15,20):0,(16,5):0,
(16,10):1,(16,55):0,(18,30):1,(19,15):0,
(19,20):1,(20,5):0,(20,10):1,(20,55):0}
global breakflag
breakflag=0
for k in dict1.keys() :
#k是字典的键,dict1[k]是它对应的值
if breakflag ==0:
itemcheck(k,dict1[k])
else :
break
while 1:
timecheck()
time.sleep(5)#倒计时1分钟时,每5秒检查一次
优化五 加入倒计时功能
from playsound import playsound
import time
breakflag=0
def ring():
#一次打铃,重复播放两次,中间间隔5秒
playsound('1.wav')
time.sleep(5)
playsound('1.wav')
def itemcheck(yz,sta):
if yz[0] == time.localtime().tm_hour and yz[1] == time.localtime().tm_min:
if sta==0:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:下课")
else:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:上课")
ring()
elif yz[0] > time.localtime().tm_hour or ( yz[0] == time.localtime().tm_hour and yz[1]>time.localtime().tm_min):
gaptime=(yz[0]-time.localtime().tm_hour-1)*60+yz[1]+(60-time.localtime().tm_min)
if sta==0:
st="下课铃"
else:
st="上课铃"
global breakflag
breakflag=1
if gaptime-1>0 :
print("距离",yz[0],":",yz[1],"的",st,"还有",gaptime,"分钟")
print("(程序休眠",gaptime-1,"分钟)")
time.sleep(gaptime*60-60)
else:
print("倒计时",60-time.localtime().tm_sec,"秒")
def timecheck():
#建立一个字典存放打铃时间和对应的状态
dict1 = {(8,0):1,(8,45):0,(8,50):1,(9,35):0,
(9,50):1,(10,35):0,(10,40):1,(11,25):0,
(11,30):1,(12,15):0,(13,30):1,(14,15):0,
(14,20):1,(15,5):0,(15,20):0,(16,5):0,
(16,10):1,(16,55):0,(18,30):1,(19,15):0,
(19,20):1,(20,5):0,(20,10):1,(20,55):0}
global breakflag
breakflag=0
for k in dict1.keys() :
#k是字典的键,dict1[k]是它对应的值
if breakflag ==0:
itemcheck(k,dict1[k])
else :
break
while 1:
#print(time.localtime().tm_hour,"时",time.localtime().tm_min,"分",time.localtime().tm_sec,"秒")
timecheck()
time.sleep(1)#倒计时1分钟时,每1秒检查一次
优化六 定时退出
from playsound import playsound
import time
import sys
breakflag=0
def ring():
#一次打铃,重复播放两次,中间间隔15秒
playsound('1.wav')
time.sleep(15)
playsound('1.wav')
def itemcheck(yz,sta):
if yz[0] == time.localtime().tm_hour and yz[1] == time.localtime().tm_min:
if sta==0:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:下课")
else:
print("当前时间:",time.localtime().tm_hour,"时",time.localtime().tm_min,"分,","状态:上课")
ring()
elif yz[0] > time.localtime().tm_hour or ( yz[0] == time.localtime().tm_hour and yz[1]>time.localtime().tm_min):
gaptime=(yz[0]-time.localtime().tm_hour-1)*60+yz[1]+(60-time.localtime().tm_min)
if sta==0:
st="下课铃"
else:
st="上课铃"
global breakflag
breakflag=1
if gaptime-1>0 :
print("距离",yz[0],":",yz[1],"的",st,"还有",gaptime,"分钟")
print("(程序休眠",gaptime-1,"分钟)")
time.sleep(gaptime*60-60)
else:
print("倒计时",60-time.localtime().tm_sec,"秒")
elif time.localtime().tm_hour >=23 and time.localtime().tm_min >= 30 and time.localtime().tm_sec > 20:
#定时退出(23:30:20)
sys.exit()
def timecheck():
#建立一个字典存放打铃时间和对应的状态
dict1 = {(8,0):1,(8,45):0,(8,50):1,(9,35):0,
(9,50):1,(10,35):0,(10,40):1,(11,25):0,
(11,30):1,(12,15):0,(13,30):1,(14,15):0,
(14,20):1,(15,5):0,(15,20):1,(16,5):0,
(16,10):1,(16,55):0,(18,30):1,(19,15):0,
(19,20):1,(20,5):0,(20,10):1,(20,55):0,
(21,30):0,(22,00):0,(23,00):0,(23,30):0}
global breakflag
breakflag=0
for k in dict1.keys() :
#k是字典的键,dict1[k]是它对应的值
if breakflag ==0:
itemcheck(k,dict1[k])
else :
break
playsound('1.wav')
time.sleep(15)
playsound('1.wav')
while 1:
timecheck()
time.sleep(1)#倒计时1分钟时,每1秒检查一次
编译
记得调整电脑休眠时间
>>铃声下载地址