写出代码:用户可以在主界面添加待做事项,所有待做事项具有四种状态:TODO、正在进行、已完成、删除状态,新创建的待做事项处于TODO状态; 2.每个待做事项可以编辑文本说明; 3. 所有待做事项(所有状态)的数据,均保存在一个文件中,程序启动时读入,每次增加、改变事项数据后,保存数据; 4. 事项状态可以随时手动修改; 5.每个事项都可以设定开始和结束的提醒日期时间(日期/小时/分钟),到达提醒时间后,播放某个.mp3文件进行提醒,同时主界面/提醒区域显示明显的提示信息;
时间: 2024-03-14 13:45:11 浏览: 118
好的,这是一个比较复杂的需求,需要涵盖很多方面。以下是一个简单的示例代码,希望能帮助你理解如何实现这个功能。
```python
import json
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
import datetime
import time
import threading
import playsound
class TodoList:
def __init__(self, master):
self.master = master
self.master.title("待办事项管理系统")
self.master.geometry("800x600")
self.master.resizable(0, 0)
self.todo_items = []
self.doing_items = []
self.done_items = []
self.deleted_items = []
self.load_data()
self.create_widgets()
def create_widgets(self):
self.notebook = ttk.Notebook(self.master)
self.notebook.pack(fill=tk.BOTH, expand=True)
self.todo_frame = tk.Frame(self.notebook)
self.doing_frame = tk.Frame(self.notebook)
self.done_frame = tk.Frame(self.notebook)
self.deleted_frame = tk.Frame(self.notebook)
self.notebook.add(self.todo_frame, text="待办事项")
self.notebook.add(self.doing_frame, text="正在进行")
self.notebook.add(self.done_frame, text="已完成")
self.notebook.add(self.deleted_frame, text="已删除")
self.create_todo_widgets()
self.create_doing_widgets()
self.create_done_widgets()
self.create_deleted_widgets()
def create_todo_widgets(self):
self.todo_table = ttk.Treeview(self.todo_frame, columns=("name", "description", "start_time", "end_time", "status", "remind"))
self.todo_table.heading("name", text="名称")
self.todo_table.heading("description", text="说明")
self.todo_table.heading("start_time", text="开始时间")
self.todo_table.heading("end_time", text="结束时间")
self.todo_table.heading("status", text="状态")
self.todo_table.heading("remind", text="提醒")
self.todo_table.column("name", width=150, anchor="center")
self.todo_table.column("description", width=250, anchor="center")
self.todo_table.column("start_time", width=150, anchor="center")
self.todo_table.column("end_time", width=150, anchor="center")
self.todo_table.column("status", width=100, anchor="center")
self.todo_table.column("remind", width=100, anchor="center")
self.todo_table.pack(fill=tk.BOTH, expand=True)
self.todo_table.bind("<Double-1>", self.edit_todo_item)
self.todo_table.bind("<Button-3>", self.popup_todo_menu)
self.todo_menu = tk.Menu(self.master, tearoff=0)
self.todo_menu.add_command(label="删除", command=self.delete_todo_item)
self.todo_menu.add_command(label="开始", command=self.start_todo_item)
self.todo_menu.add_command(label="完成", command=self.complete_todo_item)
self.todo_add_label = tk.Label(self.todo_frame, text="添加待办事项:")
self.todo_add_label.pack(side=tk.LEFT, padx=10)
self.todo_add_entry = tk.Entry(self.todo_frame, width=50)
self.todo_add_entry.pack(side=tk.LEFT, padx=10)
self.todo_add_btn = tk.Button(self.todo_frame, text="添加", command=self.add_todo_item)
self.todo_add_btn.pack(side=tk.LEFT, padx=10)
def create_doing_widgets(self):
self.doing_table = ttk.Treeview(self.doing_frame, columns=("name", "description", "start_time", "end_time", "status", "remind"))
self.doing_table.heading("name", text="名称")
self.doing_table.heading("description", text="说明")
self.doing_table.heading("start_time", text="开始时间")
self.doing_table.heading("end_time", text="结束时间")
self.doing_table.heading("status", text="状态")
self.doing_table.heading("remind", text="提醒")
self.doing_table.column("name", width=150, anchor="center")
self.doing_table.column("description", width=250, anchor="center")
self.doing_table.column("start_time", width=150, anchor="center")
self.doing_table.column("end_time", width=150, anchor="center")
self.doing_table.column("status", width=100, anchor="center")
self.doing_table.column("remind", width=100, anchor="center")
self.doing_table.pack(fill=tk.BOTH, expand=True)
self.doing_table.bind("<Double-1>", self.edit_doing_item)
self.doing_table.bind("<Button-3>", self.popup_doing_menu)
self.doing_menu = tk.Menu(self.master, tearoff=0)
self.doing_menu.add_command(label="删除", command=self.delete_doing_item)
self.doing_menu.add_command(label="暂停", command=self.pause_doing_item)
self.doing_menu.add_command(label="完成", command=self.complete_doing_item)
def create_done_widgets(self):
self.done_table = ttk.Treeview(self.done_frame, columns=("name", "description", "start_time", "end_time", "status", "remind"))
self.done_table.heading("name", text="名称")
self.done_table.heading("description", text="说明")
self.done_table.heading("start_time", text="开始时间")
self.done_table.heading("end_time", text="结束时间")
self.done_table.heading("status", text="状态")
self.done_table.heading("remind", text="提醒")
self.done_table.column("name", width=150, anchor="center")
self.done_table.column("description", width=250, anchor="center")
self.done_table.column("start_time", width=150, anchor="center")
self.done_table.column("end_time", width=150, anchor="center")
self.done_table.column("status", width=100, anchor="center")
self.done_table.column("remind", width=100, anchor="center")
self.done_table.pack(fill=tk.BOTH, expand=True)
self.done_table.bind("<Double-1>", self.edit_done_item)
self.done_table.bind("<Button-3>", self.popup_done_menu)
self.done_menu = tk.Menu(self.master, tearoff=0)
self.done_menu.add_command(label="删除", command=self.delete_done_item)
def create_deleted_widgets(self):
self.deleted_table = ttk.Treeview(self.deleted_frame, columns=("name", "description", "start_time", "end_time", "status", "remind"))
self.deleted_table.heading("name", text="名称")
self.deleted_table.heading("description", text="说明")
self.deleted_table.heading("start_time", text="开始时间")
self.deleted_table.heading("end_time", text="结束时间")
self.deleted_table.heading("status", text="状态")
self.deleted_table.heading("remind", text="提醒")
self.deleted_table.column("name", width=150, anchor="center")
self.deleted_table.column("description", width=250, anchor="center")
self.deleted_table.column("start_time", width=150, anchor="center")
self.deleted_table.column("end_time", width=150, anchor="center")
self.deleted_table.column("status", width=100, anchor="center")
self.deleted_table.column("remind", width=100, anchor="center")
self.deleted_table.pack(fill=tk.BOTH, expand=True)
self.deleted_table.bind("<Double-1>", self.edit_deleted_item)
self.deleted_table.bind("<Button-3>", self.popup_deleted_menu)
self.deleted_menu = tk.Menu(self.master, tearoff=0)
self.deleted_menu.add_command(label="恢复", command=self.restore_deleted_item)
def popup_todo_menu(self, event):
self.todo_menu.post(event.x_root, event.y_root)
def popup_doing_menu(self, event):
self.doing_menu.post(event.x_root, event.y_root)
def popup_done_menu(self, event):
self.done_menu.post(event.x_root, event.y_root)
def popup_deleted_menu(self, event):
self.deleted_menu.post(event.x_root, event.y_root)
def add_todo_item(self):
name = self.todo_add_entry.get()
if name:
item = {"name": name, "description": "", "start_time": "", "end_time": "", "status": "TODO", "remind": ""}
self.todo_items.append(item)
self.save_data()
self.show_todo_items()
def edit_todo_item(self, event):
item_id = self.todo_table.selection()[0]
item = self.todo_items[int(item_id)]
dialog = EditDialog(self.master, item)
if dialog.result:
self.todo_items[int(item_id)] = dialog.result
self.save_data()
self.show_todo_items()
def delete_todo_item(self):
item_id = self.todo_table.selection()[0]
item = self.todo_items.pop(int(item_id))
item["status"] = "DELETED"
self.deleted_items.append(item)
self.save_data()
self.show_todo_items()
def start_todo_item(self):
item_id = self.todo_table.selection()[0]
item = self.todo_items[int(item_id)]
item["status"] = "DOING"
item["start_time"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.doing_items.append(item)
self.todo_items.pop(int(item_id))
self.save_data()
self.show_todo_items()
self.show_doing_items()
def complete_todo_item(self):
item_id = self.todo_table.selection()[0]
item = self.todo_items.pop(int(item_id))
item["status"] = "DONE"
item["end_time"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.done_items.append(item)
self.save_data()
self.show_todo_items()
self.show_done_items()
def edit_doing_item(self, event):
item_id = self.doing_table.selection()[0]
item = self.doing_items[int(item_id)]
dialog = EditDialog(self.master, item)
if dialog.result:
self.doing_items[int(item_id)] = dialog.result
self.save_data()
self.show_doing_items()
def delete_doing_item(self):
item_id = self.doing_table.selection()[0]
item = self.doing_items.pop(int(item_id))
item["status"] = "DELETED"
self.deleted_items.append(item)
self.save_data()
self.show_doing_items()
def pause_doing_item(self):
item_id = self.doing_table.selection()[0]
item = self.doing_items[int(item_id)]
item["status"] = "TODO"
self.todo_items.append(item)
self.doing_items.pop(int(item_id))
self.save_data()
self.show_todo_items()
self.show_doing_items()
def complete_doing_item(self):
item_id = self.doing_table.selection()[0]
item = self.doing_items.pop(int(item_id))
item["status"] = "DONE"
item["end_time"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.done_items.append(item)
self.save_data()
self.show_doing_items()
self.show_done_items()
def edit_done_item(self, event):
item_id = self.done_table.selection()[0]
item = self.done_items[int(item_id)]
dialog = EditDialog(self.master, item)
if dialog.result:
self.done_items[int(item_id)] = dialog.result
self.save_data()
self.show_done_items()
def delete_done_item(self):
item_id = self.done_table.selection()[0]
self.done_items.pop(int(item_id))
self.save_data()
self.show_done_items()
def edit_deleted_item(self, event):
item_id = self.deleted_table.selection()[0]
item = self.deleted_items[int(item_id)]
dialog = EditDialog(self.master, item)
if dialog.result:
self.deleted_items[int(item_id)] = dialog.result
self.save_data()
self.show_deleted_items()
def restore_deleted_item(self):
item_id = self.deleted_table.selection()[0]
item = self.deleted_items.pop(int(item_id))
item["status"] = "TODO"
self.todo_items.append(item)
self.save_data()
self.show_deleted_items()
self.show_todo_items()
def load_data(self):
try:
with open("todo_data.json", "r") as f:
data = json.load(f)
self.todo_items = data["todo_items"]
self.doing_items = data["doing_items"]
self.done_items = data["done_items"]
self.deleted_items = data["deleted_items"]
except FileNotFoundError:
pass
def save_data(self):
data = {"todo_items": self.todo_items, "doing_items": self.doing_items, "done_items": self.done_items, "deleted_items": self.deleted_items}
with open("todo_data.json", "w") as f:
json.dump(data, f)
def show_todo_items(self):
self.todo_table.delete(*self.todo_table.get_children())
for i, item in enumerate(self.todo_items):
self.todo_table.insert("", i, text=i, values=(item["name"], item["description"], item["start_time"], item["end_time"], item["status"], item["remind"]))
def show_doing_items(self):
self.doing_table.delete(*self.doing_table.get_children())
for i, item in enumerate(self.doing_items):
self.doing_table.insert("", i, text=i, values=(item["name"], item["description"], item["start_time"], item["end_time"], item["status"], item["remind"]))
def show_done_items(self):
self.done_table.delete(*self.done_table.get_children())
for i, item in enumerate(self.done_items):
self.done_table.insert("", i, text=i, values=(item["name"], item["description"], item["start_time"], item["end_time"], item["status"], item["remind"]))
def show_deleted_items(self):
self.deleted_table.delete(*self.deleted_table.get_children())
for i, item in enumerate(self.deleted_items):
self.deleted_table.insert("", i, text=i, values=(
阅读全文