Python 自动学习提醒小程序 - 每隔十分钟提醒你认真学习
Python 自动学习提醒小程序 - 每隔十分钟提醒你认真学习
这个小程序可以每隔十分钟弹出一个窗口提醒你认真学习,帮助你保持专注,提高学习效率。
代码示例
import time
import tkinter as tk
def reminder():
root = tk.Tk()
root.withdraw()
root.attributes('-topmost', True)
root.after(0, lambda: root.focus_force())
root.title('学习提醒')
message = '该认真学习啦!'
tk.messagebox.showinfo(title='学习提醒', message=message)
root.destroy()
while True:
reminder()
time.sleep(600) # 10分钟=600秒
代码解释
-
导入必要的模块:
time用于等待一段时间。tkinter用于显示提醒窗口。
-
定义
reminder函数:- 该函数使用
tkinter.messagebox.showinfo来显示一个信息框,其中包含提醒消息。 - 使用
Tk类创建一个隐藏的窗口 (root.withdraw()),并将其设置为始终在顶部 (root.attributes('-topmost', True))。 - 使用
after方法确保窗口在显示之前获得焦点 (root.after(0, lambda: root.focus_force())) - 最后,使用
tk.messagebox.showinfo显示提醒信息,并在窗口关闭后销毁窗口 (root.destroy())
- 该函数使用
-
使用
while循环来每隔10分钟调用reminder函数:time.sleep(600)暂停10分钟,然后再次调用reminder函数。
如何运行
- 将代码保存为
.py文件(例如reminder.py)。 - 打开命令行或终端,进入代码所在目录。
- 运行命令
python reminder.py。
现在你应该看到每隔十分钟弹出一个提醒窗口,帮助你保持学习状态!
原文地址: https://www.cveoy.top/t/topic/mOii 著作权归作者所有。请勿转载和采集!