给我写一个备忘录小程序
时间: 2024-03-10 13:49:23 浏览: 143
好的,以下是一个备忘录小程序的示例代码,使用了Python语言和Tkinter库来实现:
```python
import tkinter as tk
class MemoPad:
def __init__(self, master):
self.master = master
self.master.title("备忘录")
self.master.geometry("400x400")
self.memo_text = tk.Text(self.master, height=20, width=50)
self.memo_text.pack()
self.save_button = tk.Button(self.master, text="保存", command=self.save_memo)
self.save_button.pack()
self.clear_button = tk.Button(self.master, text="清空", command=self.clear_memo)
self.clear_button.pack()
self.load_button = tk.Button(self.master, text="载入", command=self.load_memo)
self.load_button.pack()
def save_memo(self):
memo_content = self.memo_text.get("1.0", "end-1c")
with open("memo.txt", "w") as f:
f.write(memo_content)
print("已保存备忘录")
def clear_memo(self):
self.memo_text.delete("1.0", "end")
print("已清空备忘录")
def load_memo(self):
try:
with open("memo.txt", "r") as f:
memo_content = f.read()
self.memo_text.delete("1.0", "end")
self.memo_text.insert("1.0", memo_content)
print("已载入备忘录")
except FileNotFoundError:
print("备忘录文件不存在")
if __name__ == "__main__":
root = tk.Tk()
memopad = MemoPad(root)
root.mainloop()
```
这个小程序提供了三个功能:保存备忘录、清空备忘录和载入备忘录。用户可以在文本框中输入备忘内容,然后点击保存按钮将备忘录保存到文件中;点击清空按钮可以清空文本框中的内容;点击载入按钮可以从文件中读取备忘录并显示在文本框中。这个小程序简单易用,可以帮助用户方便地记录和管理备忘内容。
阅读全文