基于Python的桌面定时提醒小程序
import tkinter as tk
from threading import Timer
from datetime import datetime
def show_reminder(text):
def timer_handler():
top = tk.Toplevel(root)
label = tk.Label(top, text=text, bg='yellow', font='Verdana 10', pack=1)
label.pack(expand=1, fill=tk.BOTH)
time_str = entry_time.get()
if time_str:
reminder_time = datetime.strptime(time_str, '%H:%M:%S')
delay_seconds = (reminder_time - datetime.now()).total_seconds()
if delay_seconds > 0:
Timer(delay_seconds, timer_handler).start()
else:
show_reminder('Invalid time format or time in the past!')
root = tk.Tk()
root.title('Reminder')
label_time = tk.Label(root, text='Enter time (HH:MM:SS):', font='Verdana 10')
label_time.pack()
entry_time = tk.Entry(root, font='Verdana 10')
entry_time.pack()
button_ok = tk.Button(root, text='Set Reminder', font='Verdana 10', command=lambda: show_reminder('Reminder: ' + entry_time.get()))
button_ok.pack()
root.mainloop()
这段代码使用了tkinter库来创建一个简单的图形用户界面,并通过threading库的Timer函数来实现定时提醒功能。用户可以在输入框中输入一个时间,然后点击按钮设置提醒。当时间到达时,会弹出一个提醒窗口。这个例子简单易懂,适合作为学习如何在Python中创建简单桌面提醒应用的起点。
评论已关闭