关键词搜索

源码搜索 ×
×

Python实用工具,SimpleGUICS2Pygame模块,Python实现简易版计时器

发布2021-08-06浏览397次

详情内容

开发工具

Python版本:3.6.4

相关模块:

SimpleGUICS2Pygame模块。

计时器

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

原理简介

内容比较简单,就简单介绍一下吧。

首先创建主界面和一个计时器:

  1. def main():
  2. global t, color
  3. t = 0
  4. color = 'white'
  5. frame = simplegui.create_frame('Timer', 200, 200, 150)
  6. # 1000 / 100 = 10, 即t自加10次为一秒
  7. global timer
  8. timer = simplegui.create_timer(100, timerHandler)
  9. frame.set_draw_handler(drawHandler)
  10. button_start = frame.add_button('Start', Start, 150)
  11. button_stop = frame.add_button('Stop', Stop, 150)
  12. button_clear = frame.add_button('Clear', Clear, 150)
  13. frame.start()
  14. if __name__ == '__main__':
  15. main()

t每计数十次为一秒,因此将t转换为分秒格式的代码实现如下:

  1. '''
  2. Function:
  3. 将时间转为<A:BC.D>格式
  4. '''
  5. def Convert(t):
  6. D = t % 10
  7. # 十位
  8. B = (t // 100) % 6
  9. # 个位
  10. C = (t // 10) % 10
  11. # 分钟
  12. A = t // 600
  13. return str(A) + ':' + str(B) + str(C) + '.' + str(D)

剩下的内容就是实现开始计时,结束计时,清空当前计时和将计时绘制在主界面上了:

  1. Function:
  2. 开始计时
  3. '''
  4. def Start():
  5. global timer, color
  6. color = 'white'
  7. if not timer.is_running():
  8. timer.start()
  9. '''
  10. Function:
  11. 停止计时
  12. '''
  13. def Stop():
  14. global timer, color
  15. timer.stop()
  16. color = 'red'
  17. '''
  18. Function:
  19. 清空
  20. '''
  21. def Clear():
  22. global t, timer, color
  23. timer.stop()
  24. t = 0
  25. color = 'white'
  26. '''
  27. Function:
  28. 计时器
  29. '''
  30. def timerHandler():
  31. global t
  32. t += 1
  33. '''
  34. Function:
  35. 绘制时间
  36. '''
  37. def drawHandler(canvas):
  38. t_convert = Convert(t)
  39. canvas.draw_text(t_convert, (25, 120), 60, color, 'serif')

文章到这里就结束了,感谢你的观看,下篇文章分享简易的计算器

为了感vb.net教程谢读者们,我想把我c#教程最近收藏的一些编python教程程干货分享给大家,回馈每一个读者,希望能帮到你们。

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载