-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote_app.py
More file actions
84 lines (67 loc) · 2.28 KB
/
note_app.py
File metadata and controls
84 lines (67 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import tkinter as tk
from tkinter import messagebox, simpledialog
import json
import os
NOTES_FILE = "notes.json"
def load_notes():
if os.path.exists(NOTES_FILE):
with open(NOTES_FILE, "r", encoding="utf-8") as f:
return json.load(f)
return {}
def save_notes():
with open(NOTES_FILE, "w", encoding="utf-8") as f:
json.dump(notes, f, indent=4)
def add_note():
title = simpledialog.askstring("New Note", "Enter note title:")
if title and title not in notes:
notes[title] = ""
update_listbox()
listbox.selection_clear(0, tk.END)
listbox.selection_set(tk.END)
load_selected_note()
def delete_note():
selection = listbox.curselection()
if selection:
title = listbox.get(selection)
if messagebox.askyesno("Delete", f"Delete note '{title}'?"):
notes.pop(title, None)
update_listbox()
text.delete("1.0", tk.END)
save_notes()
def load_selected_note(event=None):
selection = listbox.curselection()
if selection:
title = listbox.get(selection)
text.delete("1.0", tk.END)
text.insert(tk.END, notes.get(title, ""))
def save_current_note():
selection = listbox.curselection()
if selection:
title = listbox.get(selection)
notes[title] = text.get("1.0", tk.END).strip()
save_notes()
messagebox.showinfo("Saved", f"Note '{title}' saved!")
def update_listbox():
listbox.delete(0, tk.END)
for title in notes:
listbox.insert(tk.END, title)
# Load or create notes
notes = load_notes()
# GUI setup
root = tk.Tk()
root.title("Simple Note App")
root.geometry("600x400")
frame = tk.Frame(root)
frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
listbox = tk.Listbox(frame, width=30)
listbox.pack(side=tk.LEFT, fill=tk.Y)
listbox.bind("<<ListboxSelect>>", load_selected_note)
text = tk.Text(frame)
text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
btn_frame = tk.Frame(root)
btn_frame.pack(pady=5)
tk.Button(btn_frame, text="Add Note", command=add_note).pack(side=tk.LEFT, padx=5)
tk.Button(btn_frame, text="Delete Note", command=delete_note).pack(side=tk.LEFT, padx=5)
tk.Button(btn_frame, text="Save", command=save_current_note).pack(side=tk.LEFT, padx=5)
update_listbox()
root.mainloop()