import tkinter as tk from tkinter import messagebox, simpledialog from tkinter import ttk import json from datetime import datetime class HomeworkManager: def __init__(self, root): self.root = root self.root.title("作业管理系统") self.homework_list = [] self.frame = tk.Frame(self.root) self.frame.pack(pady=10) self.label = tk.Label(self.frame, text="作业列表") self.label.pack() self.tree = ttk.Treeview(self.frame, columns=("描述", "截止日期", "优先级", "状态"), show="headings") self.tree.heading("描述", text="描述") self.tree.heading("截止日期", text="截止日期") self.tree.heading("优先级", text="优先级") self.tree.heading("状态", text="状态") self.tree.pack(pady=10) self.add_button = tk.Button(self.frame, text="添加作业", command=self.add_homework) self.add_button.pack(side=tk.LEFT, padx=10) self.delete_button = tk.Button(self.frame, text="删除作业", command=self.delete_homework) self.delete_button.pack(side=tk.LEFT, padx=10) self.details_button = tk.Button(self.frame, text="查看详情", command=self.view_details) self.details_button.pack(side=tk.LEFT, padx=10) self.complete_button = tk.Button(self.frame, text="标记为完成", command=self.mark_as_completed) self.complete_button.pack(side=tk.LEFT, padx=10) self.stats_button = tk.Button(self.frame, text="查看统计", command=self.view_stats) self.stats_button.pack(side=tk.LEFT, padx=10) self.save_button = tk.Button(self.frame, text="保存作业", command=self.save_homework) self.save_button.pack(side=tk.LEFT, padx=10) self.load_button = tk.Button(self.frame, text="加载作业", command=self.load_homework) self.load_button.pack(side=tk.LEFT, padx=10) def add_homework(self): description = simpledialog.askstring("输入", "输入作业描述:") if description: due_date = simpledialog.askstring("输入", "输入截止日期(YYYY-MM-DD):") priority = simpledialog.askstring("输入", "输入优先级(高/中/低):") try: due_date = datetime.strptime(due_date, '%Y-%m-%d').date() except ValueError: messagebox.showwarning("无效日期", "截止日期格式应为YYYY-MM-DD") return self.homework_list.append({"描述": description, "截止日期": str(due_date), "优先级": priority, "状态": "未完成"}) self.update_treeview() def delete_homework(self): selected_item = self.tree.selection() if selected_item: index = self.tree.index(selected_item) del self.homework_list[index] self.update_treeview() else: messagebox.showwarning("警告", "未选择任何作业进行删除") def view_details(self): selected_item = self.tree.selection() if selected_item: index = self.tree.index(selected_item) hw = self.homework_list[index] message = f"描述: {hw['描述']}\n截止日期: {hw['截止日期']}\n优先级: {hw['优先级']}\n状态: {hw['状态']}" messagebox.showinfo("作业详情", message) else: messagebox.showwarning("警告", "未选择任何作业查看详情") def mark_as_completed(self): selected_item = self.tree.selection() if selected_item: index = self.tree.index(selected_item) self.homework_list[index]['状态'] = "已完成" self.update_treeview() else: messagebox.showwarning("警告", "未选择任何作业进行标记") def view_stats(self): total_homework = len(self.homework_list) completed_homework = sum(1 for hw in self.homework_list if hw["状态"] == "已完成") message = f"总作业数: {total_homework}\n已完成作业数: {completed_homework}\n未完成作业数: {total_homework - completed_homework}" messagebox.showinfo("统计信息", message) def save_homework(self): with open("homework_list.json", "w", encoding='utf-8') as f: json.dump(self.homework_list, f, ensure_ascii=False, indent=4) messagebox.showinfo("保存", "作业列表已成功保存") def load_homework(self): try: with open("homework_list.json", "r", encoding='utf-8') as f: self.homework_list = json.load(f) self.update_treeview() messagebox.showinfo("加载", "作业列表已成功加载") except FileNotFoundError: messagebox.showwarning("加载", "未找到保存的作业列表") def update_treeview(self): self.tree.delete(*self.tree.get_children()) for hw in self.homework_list: self.tree.insert("", tk.END, values=(hw["描述"],hw["截止日期"], hw["优先级"], hw["状态"])) if __name__ == "__main__": root = tk.Tk() app = HomeworkManager(root) root.mainloop()