-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotedark.py
134 lines (101 loc) · 3.83 KB
/
notedark.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Import Modules
import tkinter as tk
from tkinter import ttk, messagebox
import json
from ttkbootstrap import Style
# Create main window
root = tk.Tk()
root.title("Note App")
root.geometry("515x515")
# Configure style
style = Style(theme="darkly")
style = ttk.Style()
# Configure font for tabs
style.configure("TNotebook.Tab", font=("TkDefaultFont", 14, "bold"))
# Set dark background color
root.configure(bg="#333333")
# Create Notebook
notebook = ttk.Notebook(root, style="TNotebook")
# Load previously saved notes
notes = {}
try:
with open("notes.json", "r") as f:
notes=json.load(f)
except FileNotFoundError:
pass
# Create and pack notebook widget
notebook = ttk.Notebook(root)
notebook.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
# Function to add new note
def add_note():
# Create frame for new note
note_frame = ttk.Frame(notebook, padding=10)
notebook.add(note_frame, text="New Note")
# Widgets for note title and content
title_label = ttk.Label(note_frame, text="Title:", foreground="white", background="#333333")
title_label.grid(row=0, column=0, padx=10, pady=10, sticky="W")
title_entry = ttk.Entry(note_frame, width=40, foreground="white", background="#444444")
title_entry.grid(row=0, column=1, padx=10, pady=10)
content_label = ttk.Label(note_frame, text="Content:", foreground="white", background="#333333")
content_label.grid(row=1, column=0, padx=10, pady=10, sticky="W")
content_entry = tk.Text(note_frame, width=40, height=10, foreground="white", background="#444444")
content_entry.grid(row=1, column=1, padx=10, pady=10)
# Function to save note
def save_note():
# Get title and content
title = title_entry.get()
content = content_entry.get("1.0", tk.END)
# Add note to dictionary
notes[title] = content.strip()
# Save notes to file (JSON)
with open("notes.json", "w") as f:
json.dump(notes, f)
# Add note to notebook
note_content = tk.Text(notebook, width=40, height=10, foreground="white", background="#444444")
note_content.insert(tk.END, content)
notebook.forget(notebook.select())
notebook.add(note_content, text=title)
# Save button
save_button = ttk.Button(note_frame, text="Save",
command=save_note, style="secondary.TButton")
save_button.grid(row=2, column=0, padx=10, pady=10)
# Function to load notes
def load_notes():
try:
with open("notes.json", "r") as f:
notes=json.load(f)
# Add each note to the notebook
for title, content in notes.items():
note_content = tk.Text(notebook, width=40, height=10)
note_content.insert(tk.END, content)
notebook.add(note_content, text=title)
except FileNotFoundError:
# Do nothing if file doesn't exists
pass
# Load notes at start
load_notes()
# Function to delete note
def delete_note():
# Get current tab index
current_tab = notebook.index(notebook.select())
# Get note title
note_title = notebook.tab(current_tab, "text")
# Confirm deletion
confirm = messagebox.askyesno("Delete Note",
f"Are you sure you want to delete {note_title}?")
if confirm:
# Remove from notebook
notebook.forget(current_tab)
# Remove from notes dict
notes.pop(note_title)
# Save notes to file
with open("notes.json", "w") as f:
json.dump(notes, f)
# Buttons
new_button = ttk.Button(root, text="New",
command=add_note, style="dark.TButton")
new_button.pack(side=tk.LEFT, padx=10, pady=10)
delete_button = ttk.Button(root, text="Delete",
command=delete_note, style="dark.TButton")
delete_button.pack(side=tk.LEFT, padx=10, pady=10)
root.mainloop()