import tkinter as tkfrom tkinter import messagebox# 创建主窗口root = tk.Tk()root.title("简易记事本")root.geometry("500x400")# 保存函数:写入txt文件def save_note(): content = text_box.get("1.0", tk.END).strip() if not content: messagebox.showwarning("提示", "内容为空,无需保存!") return try: with open("note.txt", "w", encoding="utf-8") as f: f.write(content) messagebox.showinfo("成功", "内容已保存到note.txt!") except Exception as e: messagebox.showerror("错误", f"保存失败:{str(e)}")# 清空函数:删除输入框内容def clear_note(): text_box.delete("1.0", tk.END)# 多行输入框text_box = tk.Text(root, wrap=tk.WORD, font=("宋体", 12))text_box.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)# 按钮容器btn_frame = tk.Frame(root)btn_frame.pack(pady=5)# 保存/清空按钮tk.Button(btn_frame, text="保存", command=save_note, width=10).grid(row=0, column=0, padx=10)tk.Button(btn_frame, text="清空", command=clear_note, width=10).grid(row=0, column=1, padx=10)root.mainloop()