import tkinter as tk
import sqlite3
from tkinter import messagebox
from datetime import datetime
# 创建数据库连接
conn = sqlite3.connect('notes.db')
cursor = conn.cursor()
# 创建notes表格
cursor.execute('''
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
def save_note():
content = text.get("1.0", tk.END)
if content.strip():
cursor.execute('''
INSERT INTO notes (content) VALUES (?)
''', (content,))
conn.commit()
messagebox.showinfo("保存成功", "笔记已保存")
else:
messagebox.showwarning("保存失败", "笔记内容为空")
def load_notes():
cursor.execute('''
SELECT id, content, created_at FROM notes
''')
notes = cursor.fetchall()
for note in notes:
note_id, note_content, created_at = note
text.insert(tk.END, f"Note ID: {note_id}\nCreated At: {created_at}\n{note_content}\n\n")
root = tk.Tk()
root.title("记事本")
text = tk.Text(root, wrap="word")
text.pack(expand=True, fill="both")
save_button = tk.Button(root, text="保存笔记", command=save_note)
save_button.pack()
load_notes()
root.mainloop()
# 关闭数据库连接
conn.close()