Technology Sharing

Python practice: Implementation of a visual dynamic alarm clock with alarm setting function

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina


✨✨ Welcome to visit Srlua's blog (づ ̄3 ̄)づ╭~✨✨

🌟🌟 欢迎各位亲爱的读者,感谢你们抽出宝贵的时间来阅读我的文章。

我是Srlua小谢,在这里我会分享我的知识和经验。🎥

希望在这里,我们能一起探索IT世界的奥妙,提升我们的技能。🔮

记得先点赞👍后阅读哦~ 👏👏

📘📚 所属专栏:Python

欢迎访问我的主页:Srlua小谢 获取更多信息和资源。✨✨🌙🌙

​​

​​

Table of contents

Dynamic alarm clock

PyInstaller Library

Run the example:

Set alarm:

Package the files first

Can be dragged to the desktop~

win+R, enter cmd

Enter in the command line, python clock.py 22-28

Full code:


Using Python to implement dynamic alarm clock

The main functions are as follows:
1. Use turtle and other graphics drawing libraries to draw a dynamic digital tube clock, in which the hours, minutes and seconds are distinguished by different colors.
2. Ability to set alarms through the keyboard and realize window pop-up reminders.
3. PassPyInstaller Convert the library into an executable file.

PyInstaller Library

PyInstaller is a library for packaging Python applications into standalone executables.

It can package Python scripts and related dependencies (such as libraries, resource files) into a single executable file, so that users can run the application without installing the Python interpreter or related libraries. PyInstaller can be used to easily create cross-platform executable files, supporting multiple platforms such as Windows, Mac, and Linux.

Run the example:

Set alarm:

Package the files first

Can be dragged to the desktop~

win+R, enter cmd

Enter in the command line, python clock.py 22-28

clock.py is the file name. If the file name is different, modify it according to the actual file name.
It means 22:28 reminder

Full code:

  1. from turtle import *
  2. from datetime import datetime
  3. import time
  4. import sys
  5. from tkinter import messagebox, Tk
  6. def drawGap():
  7. penup()
  8. fd(5)
  9. def drawLine(draw):
  10. drawGap()
  11. pendown() if draw else penup()
  12. fd(40)
  13. drawGap()
  14. right(90)
  15. def drawDigit(d):
  16. speed(0)
  17. drawLine(True) if d in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)
  18. drawLine(True) if d in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(False)
  19. drawLine(True) if d in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
  20. drawLine(True) if d in [0, 2, 6, 8] else drawLine(False)
  21. left(90)
  22. drawLine(True) if d in [0, 4, 5, 6, 8, 9] else drawLine(False)
  23. drawLine(True) if d in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False)
  24. drawLine(True) if d in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False)
  25. left(180)
  26. penup()
  27. fd(20)
  28. def drawDate(date):
  29. pencolor('Red')
  30. for i in date:
  31. if i == "-":
  32. write('时', font=('Arial', 24, 'normal'))
  33. pencolor('Green')
  34. fd(40)
  35. elif i == '=':
  36. write('分', font=('Arial', 24, 'normal'))
  37. pencolor('Blue')
  38. fd(40)
  39. elif i == '+':
  40. write('秒', font=('Arial', 24, 'normal'))
  41. else:
  42. drawDigit(eval(i))
  43. def main(alarm_time=None):
  44. setup(800, 350, 200, 200)
  45. hideturtle()
  46. pensize(8)
  47. tracer(False) # 设置快速模式
  48. root = Tk()
  49. root.withdraw() # 隐藏主窗口
  50. while True:
  51. clear() # 清除之前绘制的内容
  52. penup()
  53. goto(-300, 0)
  54. current_time = datetime.now().strftime('%H-%M=%S+')
  55. drawDate(current_time)
  56. update() # 手动刷新屏幕
  57. if alarm_time and current_time.startswith(alarm_time):
  58. messagebox.showinfo("闹钟提醒", "时间到了!")
  59. alarm_time = None # 清除闹钟时间,防止重复提示
  60. time.sleep(1)
  61. if __name__ == "__main__":
  62. alarm_time = None
  63. if len(sys.argv) > 1:
  64. alarm_time = sys.argv[1] # 获取命令行输入的闹钟时间,格式为 HH-MM
  65. main(alarm_time)
  66. done()

I hope this helps! Come on!

If you think the content of this article is useful, please give your approval and subscribe so that you can continue to receive valuable information. Thank you very much for your attention and support!