导语
啊哈!这几天在上班的路上每天都看到很多不知道那所高中的学生!
这不是9月中旬了嘛!都在军训,刚好在我们上班的附近,瞬间将我的记忆拉回了我大学高中军训的时光。
站军姿、蹲下、跑步、正步......腿都蹲嘛了,跑步这项真不是我的强项。
平常训练居然都是要⏲计时的,不知道军训多久了,感觉一晒就是一天,每次听到教官说时间到可以休息一下,真的不容易!
现在的话计时器也运用的特别广泛,到处都有它的身影,参加比赛等等,
今天就带大家做一款简易的Python版本的计时器!
正文
环境安装:
Python3.6、pycharm2021、SimpleGUICS2Pygame模块。
pip install -i https://pypi.douban.com/simple/ SimpleGUICS2Pygame
附代码:
# coding: utf-8
'''
主题:
简单计时器
'''
import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
'''
Function:
将时间转为<A:BC.D>格式
'''
def Convert(t):
D = t % 10
# 十位
B = (t // 100) % 6
# 个位
C = (t // 10) % 10
# 分钟
A = t // 600
return str(A) + ':' + str(B) + str(C) + '.' + str(D)
'''
Function:
开始计时
'''
def Start():
global timer, color
color = 'white'
if not timer.is_running():
timer.start()
'''
Function:
停止计时
'''
def Stop():
global timer, color
timer.stop()
color = 'red'
'''
Function:
清空
'''
def Clear():
global t, timer, color
timer.stop()
t = 0
color = 'white'
'''
Function:
计时器
'''
def timerHandler():
global t
t += 1
'''
Function:
绘制时间
'''
def drawHandler(canvas):
t_convert = Convert(t)
canvas.draw_text(t_convert, (25, 120), 60, color, 'serif')
'''
Function:
主函数
'''
def main():
global t, color
t = 0
color = 'white'
frame = simplegui.create_frame('TiME', 200, 200, 150)
# 1000 / 100 = 10, 即t自加10次为一秒
global timer
timer = simplegui.create_timer(100, timerHandler)
frame.set_draw_handler(drawHandler)
button_start = frame.add_button('Start', Start, 150)
button_stop = frame.add_button('Stop', Stop, 150)
button_clear = frame.add_button('Clear', Clear, 150)
frame.start()
if __name__ == '__main__':
main()
效果如下:
总结
好啦!一款简单的计时器就完成啦,拿去玩儿吧~制作不易,记得一键三连哦!!
如果需要本文完整的代码+图片素材。 Python新手安装包、免费激活码、等等更多Python资料 源码基地【私信小编06】即可免费领取哦!!
欢迎阅读往期文章~