import tkinter as tk from tkinter import messagebox, simpledialog, scrolledtext import sqlite3 from datetime import datetime

------------------ DATABASE ------------------

conn = sqlite3.connect("database.db") cursor = conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS notes ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, content TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL ) ''') conn.commit()

------------------ FUNCTIONS ------------------

def add_note(): title = simpledialog.askstring("Note Title", "Enter note title:") if title: content = simpledialog.askstring("Note Content", "Enter note content:") if content: now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") cursor.execute("INSERT INTO notes (title, content, created_at, updated_at) VALUES (?, ?, ?, ?)", (title, content, now, now)) conn.commit() messagebox.showinfo("Success", "Note added successfully!") load_notes() else: messagebox.showwarning("Empty Content", "Note content cannot be empty!") else: messagebox.showwarning("Empty Title", "Note title cannot be empty!")

def load_notes(search_query=""): listbox.delete(0, tk.END) if search_query: cursor.execute("SELECT id, title FROM notes WHERE title LIKE ?", ('%' + search_query + '%',)) else: cursor.execute("SELECT id, title FROM notes ORDER BY updated_at DESC") for note in cursor.fetchall(): listbox.insert(tk.END, f"{note[0]}: {note[1]}")

def view_note(): try: selected = listbox.get(listbox.curselection()) note_id = int(selected.split(":")[0]) cursor.execute("SELECT title, content, created_at, updated_at FROM notes WHERE id=?", (note_id,)) note = cursor.fetchone() if note: top = tk.Toplevel(root) top.title(note[0]) tk.Label(top, text=f"Created: {note[2]} | Updated: {note[3]}", font=("Arial", 8), fg="gray").pack() text_area = scrolledtext.ScrolledText(top, width=60, height=20, wrap=tk.WORD) text_area.pack(padx=10, pady=10) text_area.insert(tk.END, note[1]) text_area.config(state=tk.DISABLED) except: messagebox.showwarning("Select Note", "Please select a note to view!")

def delete_note(): try: selected = listbox.get(listbox.curselection()) note_id = int(selected.split(":")[0]) confirm = messagebox.askyesno("Delete Note", "Are you sure you want to delete this note?") if confirm: cursor.execute("DELETE FROM notes WHERE id=?", (note_id,)) conn.commit() messagebox.showinfo("Deleted", "Note deleted successfully!") load_notes() except: messagebox.showwarning("Select Note", "Please select a note to delete!")

def update_note(): try: selected = listbox.get(listbox.curselection()) note_id = int(selected.split(":")[0]) cursor.execute("SELECT title, content FROM notes WHERE id=?", (note_id,)) note = cursor.fetchone() if note: new_title = simpledialog.askstring("Update Title", "Enter new title:", initialvalue=note[0]) new_content = simpledialog.askstring("Update Content", "Enter new content:", initialvalue=note[1]) if new_title and new_content: now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") cursor.execute("UPDATE notes SET title=?, content=?, updated_at=? WHERE id=?", (new_title, new_content, now, note_id)) conn.commit() messagebox.showinfo("Updated", "Note updated successfully!") load_notes() except: messagebox.showwarning("Select Note", "Please select a note to update!")

def search_notes(): query = search_entry.get() load_notes(query)

------------------ GUI ------------------

root = tk.Tk() root.title("📝 NoteNest - Smart Notes Organizer") root.geometry("550x500") root.config(bg="#f5f5f5")

tk.Label(root, text="NoteNest", font=("Arial", 18, "bold"), bg="#f5f5f5", fg="#333").pack(pady=10) search_entry = tk.Entry(root, width=40, font=("Arial", 12)) search_entry.pack(pady=5) tk.Button(root, text="Search", command=search_notes, bg="#2196F3", fg="white", width=10).pack(pady=5)

listbox = tk.Listbox(root, width=60, height=15, font=("Arial", 11)) listbox.pack(pady=10)

btn_frame = tk.Frame(root, bg="#f5f5f5") btn_frame.pack()

tk.Button(btn_frame, text="Add Note", command=add_note, width=12, bg="#4CAF50", fg="white").grid(row=0, column=0, padx=5) tk.Button(btn_frame, text="View Note", command=view_note, width=12, bg="#2196F3", fg="white").grid(row=0, column=1, padx=5) tk.Button(btn_frame, text="Update Note", command=update_note, width=12, bg="#FFC107").grid(row=0, column=2, padx=5) tk.Button(btn_frame, text="Delete Note", command=delete_note, width=12, bg="#F44336", fg="white").grid(row=0, column=3, padx=5)

load_notes() root.mainloop()

Built With

Share this project:

Updates