Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
338 lines (271 loc) · 10.6 KB
/
Copy pathmain.py
File metadata and controls
338 lines (271 loc) · 10.6 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
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
338
import customtkinter as ctk
from PIL import Image
from tkinter import messagebox
import tasks
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
logo_path = os.path.join(BASE_DIR, "assets", "logo.png")
icon_path = os.path.join(BASE_DIR, "assets", "logo.ico")
def ensure_app_icon():
try:
if os.path.exists(logo_path):
Image.open(logo_path).save(
icon_path, format="ICO", sizes=[(16, 16), (32, 32), (64, 64)]
)
except (FileNotFoundError, OSError, ValueError):
if os.path.exists(icon_path):
try:
os.remove(icon_path)
except OSError:
pass
ensure_app_icon()
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
class TodoApp(ctk.CTk):
def __init__(self):
super().__init__()
self.title("TaskTracker")
self.geometry("800x500")
self.resizable(width=False, height=False)
if os.path.exists(icon_path):
self.iconbitmap(icon_path)
self.button_frame = ctk.CTkFrame(self, fg_color="transparent")
self.button_frame.grid(
row=1, column=0, columnspan=5, padx=20, pady=5, sticky="w"
)
self.task_input = ctk.CTkEntry(
self.button_frame, placeholder_text="Enter a new task...", width=450
)
self.task_input.bind("<Return>", lambda event: self.add_task_clicked())
self.add_button = ctk.CTkButton(
self.button_frame,
text="Add task",
command=self.add_task_clicked,
fg_color="#1f538d",
)
self.task_input.grid(
row=0, column=0, columnspan=3, padx=20, pady=20, sticky="w"
)
self.add_button.grid(
row=0,
column=3,
padx=10,
pady=20,
)
self.update_status_button = ctk.CTkButton(
self.button_frame,
text="Update status",
command=self.update_status_clicked,
fg_color="#1f538d",
)
self.update_status_button.grid(row=1, column=0, padx=5, pady=5)
self.is_update_mode_activate = False
self.delete_button = ctk.CTkButton(
self.button_frame,
text="Delete task",
command=self.delete_task_clicked,
fg_color="#1f538d",
)
self.delete_button.grid(row=1, column=1, padx=5, pady=5)
self.is_delete_mode_activate = False
self.delete_completed_button = ctk.CTkButton(
self.button_frame,
text="Delete completed tasks",
command=self.delete_completed_clicked,
fg_color="#1f538d",
)
self.delete_completed_button.grid(row=1, column=2, padx=5, pady=5)
self.is_delete_completed_mode_activate = False
self.delete_all_button = ctk.CTkButton(
self.button_frame,
text="Delete all tasks",
command=self.delete_all_clicked,
fg_color="#b91c1c",
hover_color="#7f1d1d",
)
self.delete_all_button.grid(row=1, column=3, padx=5, pady=5)
self.delete_all_mode_activate = False
self.status_card = ctk.CTkFrame(
self.button_frame, width=90, height=90, fg_color="#1f2937", corner_radius=12
)
self.status_card.grid_propagate(False)
self.status_card.grid(
row=0, column=4, rowspan=2, padx=(20, 0), pady=5, sticky="ns"
)
self.status_card.grid_rowconfigure((0, 1), weight=1)
self.status_card.grid_columnconfigure(0, weight=1)
self.pending_count_label = ctk.CTkLabel(
self.status_card,
text="0",
font=("helvetica", 28, "bold"),
text_color="#1f538d",
)
self.pending_count_label.grid(row=0, column=0, sticky="s", pady=(5, 0))
self.pending_text_label = ctk.CTkLabel(
self.status_card,
text="ACTIVE",
font=("helvetica", 10, "bold"),
text_color="#9ca3af",
)
self.pending_text_label.grid(row=1, column=0, sticky="n", pady=(0, 5))
self.task_scroll_frame = ctk.CTkScrollableFrame(self, width=740, height=320)
self.task_scroll_frame.grid(
row=2, column=0, columnspan=5, padx=20, pady=15, sticky="nsew"
)
self.refresh_task_display()
self.bind("<Button-1>", self.handle_global_click)
def reset_action_modes(self):
self.is_update_mode_activate = False
self.is_delete_mode_activate = False
self.is_delete_completed_mode_activate = False
self.delete_all_mode_activate = False
self.update_status_button.configure(text="Update status", fg_color="#1f538d")
self.delete_button.configure(text="Delete task", fg_color="#1f538d")
self.delete_completed_button.configure(
text="Delete completed tasks", fg_color="#1f538d"
)
self.delete_all_button.configure(text="Delete all tasks", fg_color="#b91c1c")
def handle_global_click(self, event):
if self.is_update_mode_activate or self.is_delete_mode_activate:
clicked_widget = event.widget
inside_button_frame = False
inside_task_frame = False
current_widget = clicked_widget
while current_widget is not None:
if current_widget == self.button_frame:
inside_button_frame = True
break
if current_widget == self.task_scroll_frame:
inside_task_frame = True
break
current_widget = current_widget.master
if not inside_button_frame and not inside_task_frame:
self.reset_action_modes()
self.refresh_task_display()
def update_button_states(self, tasks_database=None):
if tasks_database is None:
tasks_database = tasks.load_tasks()
has_tasks = bool(tasks_database)
state = "normal" if has_tasks else "disabled"
self.update_status_button.configure(state=state)
self.delete_button.configure(state=state)
self.delete_completed_button.configure(state=state)
self.delete_all_button.configure(state=state)
def refresh_task_display(self):
for widget in self.task_scroll_frame.winfo_children():
widget.destroy()
tasks_database = tasks.load_tasks()
self.update_button_states(tasks_database)
self.update_active_counter(tasks_database)
if not tasks_database:
empty_label = ctk.CTkLabel(
self.task_scroll_frame,
text= "No task yet - add one above to get started",
font= ("Arial", 14),
text_color= "#9ca3af"
)
empty_label.pack(pady=40)
return
for task in tasks_database:
row_panel = ctk.CTkFrame(
self.task_scroll_frame, fg_color="#302F2F", corner_radius=8, height=50
)
row_panel.pack(fill="x", pady=5, padx=5)
task_text = ctk.CTkLabel(
row_panel, text=task["task"], font=("Arial", 14), anchor="w"
)
task_text.pack(side="left", padx=20, pady=12)
badge_color = (
"#3a7ebf"
if "pending" in task["status"].lower()
else "#d97706" if "progress" in task["status"].lower() else "#2e7d32"
)
status_badge = ctk.CTkLabel(
row_panel,
text=task["status"],
fg_color=badge_color,
text_color="white",
corner_radius=6,
width=90,
height=26,
cursor="hand2",
)
status_badge.pack(side="right", padx=20, pady=12)
row_panel.bind(
"<Button-1>",
lambda event, t_id=task["id"]: self.handle_row_selection(t_id),
)
task_text.bind(
"<Button-1>",
lambda event, t_id=task["id"]: self.handle_row_selection(t_id),
)
def add_task_clicked(self):
user_task = self.task_input.get()
if not user_task.strip():
return
tasks.add_task(user_task)
self.task_input.delete(0, "end")
self.refresh_task_display()
def update_status_clicked(self):
if not tasks.load_tasks():
return
if self.is_update_mode_activate:
self.reset_action_modes()
self.refresh_task_display()
return
self.reset_action_modes()
self.is_update_mode_activate = True
self.update_status_button.configure(
text="Select a task below", fg_color="#d97706"
)
self.update_active_counter()
self.refresh_task_display()
def handle_row_selection(self, task_id):
if self.is_update_mode_activate:
tasks.update_status(task_id)
self.reset_action_modes()
self.refresh_task_display()
elif self.is_delete_mode_activate:
tasks.delete_tasks(task_id)
self.reset_action_modes()
self.refresh_task_display()
def delete_task_clicked(self):
if not tasks.load_tasks():
return
if self.is_delete_mode_activate:
self.reset_action_modes()
self.refresh_task_display()
return
self.reset_action_modes()
self.is_delete_mode_activate = True
self.delete_button.configure(text="Select a task to delete", fg_color="#b91c1c")
self.update_active_counter()
self.refresh_task_display()
def delete_completed_clicked(self):
if not tasks.load_tasks():
return
self.reset_action_modes()
tasks.delete_completed_task()
self.refresh_task_display()
self.update_active_counter()
def delete_all_clicked(self):
if not tasks.load_tasks():
return
user_confirmed = messagebox.askyesno(
title="Warning!",
message="Are you absolutely sure you want to delete ALL tasks?\nThis cannot be undone!",
)
self.reset_action_modes()
if user_confirmed:
tasks.delete_all_tasks()
self.refresh_task_display()
self.update_active_counter()
self.refresh_task_display()
def update_active_counter(self, tasks_database=None):
if tasks_database is None:
tasks_database = tasks.load_tasks()
active_count = tasks.get_active_tasks_count(tasks_database)
self.pending_count_label.configure(text=str(active_count))
if __name__ == "__main__":
app = TodoApp()
app.mainloop()