好的,我来给您编写这个程序。
首先,我们需要导入必要的模块:`tkinter`、`datetime`、`time`和`threading`。其中,`tkinter`用于构建GUI界面,`datetime`用于处理日期和时间,`time`用于等待时间,`threading`用于多线程。
```python
import tkinter as tk
from datetime import datetime, timedelta
import time
import threading
```
接下来,我们需要定义一个`Schedule`类,用于管理日程。在这个类中,我们需要定义一些属性和方法,如下:
```python
class Schedule:
def __init__(self, title, start_time, duration, interval, advance, repeat, repeat_type):
self.title = title
self.start_time = start_time
self.duration = duration
self.interval = interval
self.advance = advance
self.repeat = repeat
self.repeat_type = repeat_type
self.completed = False
self.paused = False
self.remind_thread = None
def start(self):
self.completed = False
self.paused = False
self.remind_thread = threading.Thread(target=self.remind)
self.remind_thread.start()
def pause(self):
self.paused = True
def resume(self):
self.paused = False
def stop(self):
self.completed = True
def remind(self):
while not self.completed:
if not self.paused:
now = datetime.now()
start_time = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S')
delta = start_time - now + timedelta(minutes=self.advance)
if delta.total_seconds() <= 0:
self.show_reminder()
if self.repeat:
self.repeat_reminder()
else:
self.completed = True
else:
time.sleep(delta.total_seconds())
def show_reminder(self):
duration = timedelta(minutes=self.duration)
end_time = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S') + duration
message = f"{self.title}\nStart time: {self.start_time}\nEnd time: {end_time.strftime('%Y-%m-%d %H:%M:%S')}"
root = tk.Tk()
root.title("Reminder")
label = tk.Label(root, text=message, font=('Arial', 18))
label.pack()
root.mainloop()
def repeat_reminder(self):
if self.repeat_type == 'daily':
start_time = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S') + timedelta(days=1)
self.start_time = start_time.strftime('%Y-%m-%d %H:%M:%S')
elif self.repeat_type == 'weekly':
start_time = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S') + timedelta(weeks=1)
self.start_time = start_time.strftime('%Y-%m-%d %H:%M:%S')
elif self.repeat_type == 'monthly':
month = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S').month + 1
year = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S').year
if month > 12:
month = 1
year += 1
start_time = datetime(year, month, datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S').day, datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S').hour, datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S').minute)
self.start_time = start_time.strftime('%Y-%m-%d %H:%M:%S')
elif self.repeat_type == 'yearly':
year = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S').year + 1
month = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S').month
day = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S').day
hour = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S').hour
minute = datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S').minute
start_time = datetime(year, month, day, hour, minute)
self.start_time = start_time.strftime('%Y-%m-%d %H:%M:%S')
```
在这个类中,我们定义了一个初始化方法`__init__`,用于初始化日程的属性。`start`方法用于启动日程,`pause`方法用于暂停日程,`resume`方法用于恢复日程,`stop`方法用于停止日程。`remind`方法是一个循环,用于等待日程的开始时间,并在开始时间前`advance`分钟提醒用户。如果设置了重复提醒,则在提醒后继续等待下一次提醒时间。`show_reminder`方法用于显示提醒窗口,`repeat_reminder`方法用于计算下一次重复提醒的时间。
接下来,我们需要定义一个`ScheduleManager`类,用于管理所有的日程。在这个类中,我们需要定义一些属性和方法,如下:
```python
class ScheduleManager:
def __init__(self):
self.schedules = []
self.timer_thread = None
def add_schedule(self, schedule):
self.schedules.append(schedule)
def start(self):
self.timer_thread = threading.Thread(target=self.timer)
self.timer_thread.start()
def stop(self):
for schedule in self.schedules:
schedule.stop()
self.timer_thread.join()
def timer(self):
while True:
now = datetime.now()
for schedule in self.schedules:
start_time = datetime.strptime(schedule.start_time, '%Y-%m-%d %H:%M:%S')
delta = start_time - now
if delta.total_seconds() <= 0 and not schedule.completed:
schedule.start()
time.sleep(1)
```
在这个类中,我们定义了一个初始化方法`__init__`,用于初始化管理器的属性。`add_schedule`方法用于添加日程到管理器中。`start`方法用于启动管理器,`stop`方法用于停止管理器。`timer`方法是一个循环,用于检查所有的日程是否已经到达开始时间,并启动日程。
最后,我们需要定义一个GUI界面,用于让用户输入日程的信息并添加到管理器中。在这个界面中,我们需要定义一些控件,如下:
```python
class Application:
def __init__(self, master):
self.master = master
self.master.title("Schedule Reminder")
self.master.geometry("400x400")
self.title_label = tk.Label(self.master, text="Title:", font=('Arial', 18))
self.title_label.pack()
self.title_entry = tk.Entry(self.master, font=('Arial', 18))
self.title_entry.pack()
self.start_time_label = tk.Label(self.master, text="Start Time (YYYY-MM-DD HH:MM:SS):", font=('Arial', 18))
self.start_time_label.pack()
self.start_time_entry = tk.Entry(self.master, font=('Arial', 18))
self.start_time_entry.pack()
self.duration_label = tk.Label(self.master, text="Duration (minutes):", font=('Arial', 18))
self.duration_label.pack()
self.duration_entry = tk.Entry(self.master, font=('Arial', 18))
self.duration_entry.pack()
self.interval_label = tk.Label(self.master, text="Interval (minutes):", font=('Arial', 18))
self.interval_label.pack()
self.interval_entry = tk.Entry(self.master, font=('Arial', 18))
self.interval_entry.pack()
self.advance_label = tk.Label(self.master, text="Advance (minutes):", font=('Arial', 18))
self.advance_label.pack()
self.advance_entry = tk.Entry(self.master, font=('Arial', 18))
self.advance_entry.pack()
self.repeat_label = tk.Label(self.master, text="Repeat:", font=('Arial', 18))
self.repeat_label.pack()
self.repeat_var = tk.BooleanVar()
self.repeat_checkbox = tk.Checkbutton(self.master, text="Repeat", variable=self.repeat_var, font=('Arial', 18))
self.repeat_checkbox.pack()
self.repeat_type_label = tk.Label(self.master, text="Repeat Type:", font=('Arial', 18))
self.repeat_type_label.pack()
self.repeat_type_var = tk.StringVar()
self.repeat_type_var.set("daily")
self.repeat_type_optionmenu = tk.OptionMenu(self.master, self.repeat_type_var, "daily", "weekly", "monthly", "yearly")
self.repeat_type_optionmenu.config(font=('Arial', 18))
self.repeat_type_optionmenu.pack()
self.add_button = tk.Button(self.master, text="Add", font=('Arial', 18), command=self.add_schedule)
self.add_button.pack()
self.schedule_manager = ScheduleManager()
def add_schedule(self):
title = self.title_entry.get()
start_time = self.start_time_entry.get()
duration = int(self.duration_entry.get())
interval = int(self.interval_entry.get())
advance = int(self.advance_entry.get())
repeat = self.repeat_var.get()
repeat_type = self.repeat_type_var.get()
schedule = Schedule(title, start_time, duration, interval, advance, repeat, repeat_type)
self.schedule_manager.add_schedule(schedule)
def start(self):
self.schedule_manager.start()
```
在这个界面中,我们定义了一个初始化方法`__init__`,用于初始化界面的控件。`add_schedule`方法用于获取用户输入的日程信息并添加到管理器中。`start`方法用于启动管理器。
最后,我们需要创建一个`Application`对象并启动界面:
```python
root = tk.Tk()
app = Application(root)
app.start()
root.mainloop()
```
这样,我们就完成了这个程序的编写。完整的代码如下: