python写以下代码:用户可以在主界面添加待做事项,所有待做事项具有四种状态:TODO、正在进行、已完成、删除状态,新创建的待做事项处于TODO状态; 2.每个待做事项可以编辑文本说明; 3. 所有待做事项(所有状态)的数据,均保存在一个文件中,程序启动时读入,每次增加、改变事项数据后,保存数据; 4. 事项状态可以随时手动修改; 5.每个事项都可以设定开始和结束的提醒日期时间(日期/小时/分钟),到达提醒时间后,播放某个.mp3文件进行提醒,同时主界面/提醒区域显示明显的提示信息;
时间: 2024-03-14 13:44:55 浏览: 64
todo-list:一个简单的待办事项清单
下面是一个简单的 Python 代码示例,可实现您所描述的待办事项应用程序。请注意,这只是一个基本示例,您可能需要根据自己的需求进行更改和扩展。
```python
import datetime
import json
import os
import time
import winsound
FILENAME = "todos.json"
class TodoItem:
def __init__(self, text):
self.text = text
self.status = "TODO"
self.start_time = None
self.end_time = None
def __str__(self):
return f"{self.text} ({self.status})"
class TodoList:
def __init__(self):
self.items = []
def add_item(self, text):
item = TodoItem(text)
self.items.append(item)
def remove_item(self, index):
del self.items[index]
def edit_item(self, index, text):
self.items[index].text = text
def change_status(self, index, status):
self.items[index].status = status
def set_start_time(self, index, start_time):
self.items[index].start_time = start_time
def set_end_time(self, index, end_time):
self.items[index].end_time = end_time
def save(self):
with open(FILENAME, "w") as f:
data = {
"items": [vars(item) for item in self.items]
}
json.dump(data, f)
def load(self):
if os.path.exists(FILENAME):
with open(FILENAME, "r") as f:
data = json.load(f)
self.items = [TodoItem(**item_data) for item_data in data["items"]]
def show_items(self):
for i, item in enumerate(self.items):
print(f"{i+1}. {str(item)}")
def start_reminders(self):
while True:
now = datetime.datetime.now()
for item in self.items:
start_time = item.start_time
end_time = item.end_time
if start_time and start_time <= now and item.status != "DONE":
item.status = "IN PROGRESS"
self.save()
winsound.PlaySound("reminder.mp3", winsound.SND_FILENAME | winsound.SND_ASYNC)
print(f"Reminder: {item.text} has started!")
elif end_time and end_time <= now and item.status == "IN PROGRESS":
item.status = "DONE"
self.save()
winsound.PlaySound("reminder.mp3", winsound.SND_FILENAME | winsound.SND_ASYNC)
print(f"Reminder: {item.text} is done!")
time.sleep(60) # check every minute
def main():
todo_list = TodoList()
todo_list.load()
while True:
print("""
TODO List
---------
1. Add item
2. Remove item
3. Edit item
4. Change status
5. Set start time
6. Set end time
7. Show items
8. Start reminders
9. Quit
""")
choice = input("Enter your choice: ")
if choice == "1":
text = input("Enter task description: ")
todo_list.add_item(text)
todo_list.save()
elif choice == "2":
index = int(input("Enter index of item to remove: ")) - 1
todo_list.remove_item(index)
todo_list.save()
elif choice == "3":
index = int(input("Enter index of item to edit: ")) - 1
text = input("Enter new task description: ")
todo_list.edit_item(index, text)
todo_list.save()
elif choice == "4":
index = int(input("Enter index of item to change status: ")) - 1
status = input("Enter new status (TODO, IN PROGRESS, DONE): ")
todo_list.change_status(index, status)
todo_list.save()
elif choice == "5":
index = int(input("Enter index of item to set start time: ")) - 1
start_time = input("Enter start time (YYYY-MM-DD HH:MM): ")
start_time = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M")
todo_list.set_start_time(index, start_time)
todo_list.save()
elif choice == "6":
index = int(input("Enter index of item to set end time: ")) - 1
end_time = input("Enter end time (YYYY-MM-DD HH:MM): ")
end_time = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M")
todo_list.set_end_time(index, end_time)
todo_list.save()
elif choice == "7":
todo_list.show_items()
elif choice == "8":
todo_list.start_reminders()
elif choice == "9":
print("Goodbye!")
break
else:
print("Invalid choice.")
if __name__ == "__main__":
main()
```
该代码使用了自定义的 `TodoList` 和 `TodoItem` 类来表示待办事项和待办事项列表,并使用 JSON 文件来保存所有待办事项。在主程序中,用户可以通过选择菜单选项来添加、删除、编辑、更改状态和设置提醒时间等操作。在启动提醒功能后,程序会每分钟检查一次所有待办事项的状态,并在必要时播放提醒音频文件。
阅读全文