-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
337 lines (289 loc) · 11.2 KB
/
app.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import tkinter as tk
from tkinter import messagebox, ttk
from tkinter import filedialog
from pathlib import Path
class ModernFrame(tk.Frame):
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs)
self.configure(
bg="#1e1e1e", # Dark background
highlightbackground="#2d2d2d", # Darker border
highlightthickness=1,
relief="flat",
padx=15,
pady=15
)
class NoteTakingApp:
def __init__(self, root):
self.root = root
self.root.title("Modern Note Taking App")
# Initialize theme
self.is_dark_theme = True
# Load Dosis font
self.custom_font = ("Dosis", 12)
self.title_font = ("Dosis", 12, "bold")
# Set modern styling
self.style = ttk.Style()
self.style.configure(
"Modern.TButton",
padding=10,
relief="flat",
background="#2d2d2d",
foreground="#ffffff",
borderwidth=0,
font=self.custom_font
)
self.note_counter = 1
self.current_note_file = f"note_{self.note_counter}.txt"
# Create UI elements
self.create_ui()
# Now set the theme after UI elements are created
self.set_theme()
def create_ui(self):
# Create main container with rounded corners
self.container = ModernFrame(self.root, padx=20, pady=20)
self.container.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
# Create horizontal split container
self.split_container = tk.PanedWindow(
self.container,
orient=tk.HORIZONTAL,
bg="#1e1e1e",
sashwidth=4,
sashrelief="flat",
borderwidth=0
)
self.split_container.pack(fill=tk.BOTH, expand=True)
# Create frames with rounded corners
self.active_frame = ModernFrame(self.split_container)
self.pinned_frame = ModernFrame(self.split_container)
# Initially only add the active frame
self.split_container.add(self.active_frame, width=800) # Full width initially
# Create labels and text areas
self.create_note_areas()
# Create buttons
self.create_buttons()
def create_note_areas(self):
# Active note label
self.active_label = tk.Label(
self.active_frame,
text="Active Note",
font=self.title_font,
bg="#1e1e1e",
fg="#ffffff"
)
self.active_label.pack(pady=(0, 10))
# Active text area
self.text_area = tk.Text(
self.active_frame,
height=15,
width=40,
bg="#2d2d2d",
fg="#ffffff",
insertbackground="#ffffff",
relief="flat",
font=self.custom_font,
padx=10,
pady=10
)
self.text_area.pack(fill=tk.BOTH, expand=True)
# Pinned note label
self.pinned_label = tk.Label(
self.pinned_frame,
text="Pinned Notes",
font=self.title_font,
bg="#1e1e1e",
fg="#ffffff"
)
self.pinned_label.pack(pady=(0, 10))
# Pinned text area
self.pinned_text = tk.Text(
self.pinned_frame,
height=15,
width=40,
bg="#2d2d2d",
fg="#ffffff",
relief="flat",
font=self.custom_font,
padx=10,
pady=10,
state='disabled'
)
self.pinned_text.pack(fill=tk.BOTH, expand=True)
def set_theme(self):
if self.is_dark_theme:
bg_color = "#1e1e1e"
text_color = "#ffffff"
text_bg = "#2d2d2d"
button_frame_bg = "#2d2d2d"
# Button colors for dark theme
self.save_button.configure(bg="#90EE90", fg="#000000")
self.open_button.configure(bg="#FFA500", fg="#000000")
self.pin_button.configure(bg="#ADD8E6", fg="#000000")
self.delete_button.configure(bg="#FF6B6B", fg="#ffffff")
self.theme_button.configure(bg="#2d2d2d", fg="#ffffff", text="Dark Theme")
else:
bg_color = "#ffffff"
text_color = "#000000"
text_bg = "#f0f0f0"
button_frame_bg = "#f0f0f0"
# Button colors for light theme
self.save_button.configure(bg="#98FB98", fg="#000000") # Lighter green
self.open_button.configure(bg="#FFB84D", fg="#000000") # Lighter orange
self.pin_button.configure(bg="#B0E0E6", fg="#000000") # Lighter blue
self.delete_button.configure(bg="#FFB6B6", fg="#000000") # Lighter red
self.theme_button.configure(bg="#f0f0f0", fg="#000000", text="Light Theme")
# Configure frame backgrounds
self.root.configure(bg=bg_color)
self.container.configure(bg=bg_color)
self.split_container.configure(bg=bg_color)
self.active_frame.configure(bg=bg_color)
self.pinned_frame.configure(bg=bg_color)
self.button_frame.configure(bg=button_frame_bg) # Update button frame background
# Configure text elements
self.active_label.configure(bg=bg_color, fg=text_color)
self.pinned_label.configure(bg=bg_color, fg=text_color)
self.text_area.configure(bg=text_bg, fg=text_color, insertbackground=text_color)
self.pinned_text.configure(bg=text_bg, fg=text_color)
def toggle_theme(self):
self.is_dark_theme = not self.is_dark_theme
self.set_theme()
# Update theme button text
self.theme_button.configure(
text="Dark Theme" if self.is_dark_theme else "Light Theme"
)
def pin_note(self):
content = self.text_area.get("1.0", tk.END).strip()
if not content:
messagebox.showwarning("Warning", "Cannot pin empty note!")
return
# Add pinned frame if it's not already visible
if len(self.split_container.panes()) == 1:
self.split_container.add(self.pinned_frame, width=400)
# Adjust active frame width
self.split_container.paneconfig(self.active_frame, width=400)
self.pinned_text.configure(state='normal')
self.pinned_text.delete("1.0", tk.END)
self.pinned_text.insert("1.0", self.text_area.get("1.0", tk.END))
self.pinned_text.configure(state='disabled')
self.delete_button.configure(state='normal')
self.text_area.delete("1.0", tk.END)
messagebox.showinfo("Success", "Note pinned successfully!")
def save_note(self):
note_content = self.text_area.get("1.0", tk.END).strip()
if not note_content:
messagebox.showwarning("Warning", "Cannot save empty note!")
return
try:
with open(self.current_note_file, "w", encoding='utf-8') as f:
f.write(note_content)
messagebox.showinfo("Success", f"Note saved successfully as {self.current_note_file}!")
except Exception as e:
messagebox.showerror("Error", f"Failed to save note: {str(e)}")
def clear_note(self):
self.text_area.delete("1.0", tk.END)
def load_notes(self):
# Show file selection dialog
file_path = filedialog.askopenfilename(
title="Open Note",
filetypes=[("Text files", "*.txt")],
initialdir="."
)
if file_path:
try:
with open(file_path, "r", encoding='utf-8') as f:
content = f.read()
self.text_area.delete("1.0", tk.END)
self.text_area.insert("1.0", content)
messagebox.showinfo("Success", f"Note loaded successfully from {file_path}!")
except Exception as e:
messagebox.showerror("Error", f"Failed to load note: {str(e)}")
def show_active_note_frame(self):
self.pinned_frame.pack_forget()
self.active_frame.pack(fill=tk.BOTH, expand=True)
def show_pinned_note_frame(self):
self.active_frame.pack_forget()
self.pinned_frame.pack(fill=tk.BOTH, expand=True)
def on_hover(self, event, button, color):
if button['state'] != 'disabled':
button.configure(bg=color)
def on_leave(self, event, button, color):
if button['state'] != 'disabled':
button.configure(bg=color)
def delete_pinned_note(self):
self.pinned_text.configure(state='normal')
self.pinned_text.delete("1.0", tk.END)
self.pinned_text.configure(state='disabled')
self.delete_button.configure(state='disabled')
# Remove pinned frame and adjust active frame width
self.split_container.remove(self.pinned_frame)
self.split_container.paneconfig(self.active_frame, width=800)
# Increment counter for next note
self.note_counter += 1
self.current_note_file = f"note_{self.note_counter}.txt"
messagebox.showinfo("Success", "Pinned note deleted successfully!")
def create_buttons(self):
# Create button frame
self.button_frame = ModernFrame(self.container)
# Make it instance variable
self.button_frame.pack(fill=tk.X, pady=(10, 0))
# Save button (light green)
self.save_button = tk.Button(
self.button_frame,
text="Save",
command=self.save_note,
relief="flat",
font=self.custom_font,
padx=15,
pady=5
)
self.save_button.pack(side=tk.LEFT, padx=5)
# Open saved notes button (orange)
self.open_button = tk.Button(
self.button_frame,
text="Open Notes",
command=self.load_notes,
relief="flat",
font=self.custom_font,
padx=15,
pady=5
)
self.open_button.pack(side=tk.LEFT, padx=5)
# Pin button (light blue)
self.pin_button = tk.Button(
self.button_frame,
text="Pin Note",
command=self.pin_note,
relief="flat",
font=self.custom_font,
padx=15,
pady=5
)
self.pin_button.pack(side=tk.LEFT, padx=5)
# Delete button (red)
self.delete_button = tk.Button(
self.button_frame,
text="Delete",
command=self.delete_pinned_note,
relief="flat",
font=self.custom_font,
padx=15,
pady=5,
state='disabled'
)
self.delete_button.pack(side=tk.LEFT, padx=5)
# Theme toggle button
self.theme_button = tk.Button(
self.button_frame,
text="Dark Theme",
command=self.toggle_theme,
relief="flat",
font=self.custom_font,
padx=15,
pady=5
)
self.theme_button.pack(side=tk.RIGHT, padx=5)
if __name__ == "__main__":
root = tk.Tk()
root.geometry("800x600")
app = NoteTakingApp(root)
root.mainloop()