-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskCanter.py
286 lines (232 loc) · 10.2 KB
/
TaskCanter.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
import tkinter as tk
from tkinter import ttk, messagebox
from tkcalendar import Calendar
import sqlite3
from datetime import datetime, timedelta
def create_table():
connection = sqlite3.connect("tasks.db")
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task TEXT NOT NULL,
priority TEXT NOT NULL,
due_date TEXT,
status TEXT NOT NULL
)
""")
connection.commit()
connection.close()
def add_task():
task = entry.get()
priority = priority_var.get()
due_date = due_date_var.get()
if task:
task_display = f"{task} - Priority: {priority} - Due Date: {due_date} - Not Completed"
listbox.insert("", "end", values=(task, priority, due_date, "Not Completed"))
save_task_to_database(task, priority, due_date, "Not Completed")
set_deadline_notification(task, due_date)
entry.delete(0, tk.END)
priority_var.set("Low")
due_date_var.set("")
def remove_task():
selected_item = listbox.selection()
if selected_item:
confirmation = messagebox.askyesno("Confirmation", "Are you sure you want to remove this task?")
if confirmation:
task_id = selected_item[0]
remove_task_from_database(task_id)
listbox.delete(selected_item)
def complete_task():
selected_item = listbox.selection()
if selected_item:
task_id = selected_item[0]
complete_task_in_database(task_id)
task = listbox.item(selected_item, 'values')
if task[3] == "Not Completed":
updated_task_display = f"{task[0]} - Priority: {task[1]} - Due Date: {task[2]} - Completed"
listbox.item(selected_item, values=(task[0], task[1], task[2], "Completed"))
set_deadline_notification(task[0], task[2])
def save_tasks():
tasks = listbox.get_children()
with open("tasks.txt", "w") as file:
for task_id in tasks:
task_values = listbox.item(task_id, 'values')
file.write(f"{task_values[0]} - Priority: {task_values[1]} - Due Date: {task_values[2]} - {task_values[3]}\n")
messagebox.showinfo("Saved", "Tasks saved successfully!")
def load_tasks():
try:
with open("tasks.txt", "r") as file:
tasks = [line.strip() for line in file.readlines()]
listbox.delete(*listbox.get_children())
for task in tasks:
task_parts = task.split(" - ")
listbox.insert("", "end", values=(task_parts[0], task_parts[2], task_parts[4], task_parts[6]))
messagebox.showinfo("Loaded", "Tasks loaded successfully!")
except FileNotFoundError:
messagebox.showwarning("File Not Found", "No saved tasks found.")
def sort_tasks():
tasks = listbox.get_children()
tasks = sorted(tasks, key=lambda task_id: (listbox.item(task_id, 'values')[3], listbox.item(task_id, 'values')[1]))
listbox.delete(*listbox.get_children())
for task_id in tasks:
task_values = listbox.item(task_id, 'values')
listbox.insert("", "end", values=(task_values[0], task_values[1], task_values[2], task_values[3]))
def filter_tasks():
selected_filter = filter_var.get()
tasks = listbox.get_children()
filtered_tasks = []
if selected_filter == "All":
filtered_tasks = tasks
elif selected_filter == "Completed":
filtered_tasks = [task_id for task_id in tasks if listbox.item(task_id, 'values')[3] == "Completed"]
elif selected_filter == "Not Completed":
filtered_tasks = [task_id for task_id in tasks if listbox.item(task_id, 'values')[3] == "Not Completed"]
listbox.delete(*listbox.get_children())
for task_id in filtered_tasks:
task_values = listbox.item(task_id, 'values')
listbox.insert("", "end", values=(task_values[0], task_values[1], task_values[2], task_values[3]))
def open_calendar():
def set_due_date():
due_date_var.set(cal.get_date())
top.destroy()
top = tk.Toplevel(root)
cal = Calendar(top, selectmode="day", year=2023, month=11, day=26)
cal.pack(padx=20, pady=20)
set_button = tk.Button(top, text="Set Due Date", command=set_due_date)
set_button.pack(pady=10)
def set_deadline_notification(task, due_date):
try:
due_date_object = datetime.strptime(due_date, "%Y-%m-%d").date()
today = datetime.now().date()
days_until_due = (due_date_object - today).days
if days_until_due >= 0:
root.after(days_until_due * 86400000, show_notification, task, due_date)
except ValueError:
pass
def show_notification(task, due_date):
messagebox.showinfo("Deadline Notification", f"The task '{task}' is due on {due_date}!")
def get_task_id(selected_item):
return selected_item[0]
def save_task_to_database(task, priority, due_date, status):
connection = sqlite3.connect("tasks.db")
cursor = connection.cursor()
cursor.execute("INSERT INTO tasks (task, priority, due_date, status) VALUES (?, ?, ?, ?)", (task, priority, due_date, status))
connection.commit()
connection.close()
def remove_task_from_database(task_id):
connection = sqlite3.connect("tasks.db")
cursor = connection.cursor()
cursor.execute("DELETE FROM tasks WHERE id=?", (task_id,))
connection.commit()
connection.close()
def complete_task_in_database(task_id):
connection = sqlite3.connect("tasks.db")
cursor = connection.cursor()
cursor.execute("UPDATE tasks SET status='Completed' WHERE id=?", (task_id,))
connection.commit()
connection.close()
def edit_task():
selected_item = listbox.selection()
if selected_item:
task_id = get_task_id(selected_item)
top = tk.Toplevel(root)
top.title("Edit Task")
edited_task_var = tk.StringVar()
edited_task_var.set(listbox.item(selected_item, 'values')[0])
edited_task_label = tk.Label(top, text="Edit Task:")
edited_task_label.pack()
edited_task_entry = tk.Entry(top, textvariable=edited_task_var, width=30)
edited_task_entry.pack(pady=10)
priority_label = tk.Label(top, text="Priority:")
priority_label.pack()
edited_priority_var = tk.StringVar()
edited_priority_var.set(listbox.item(selected_item, 'values')[1])
priority_dropdown = tk.OptionMenu(top, edited_priority_var, "Low", "Medium", "High")
priority_dropdown.pack()
due_date_label = tk.Label(top, text="Due Date:")
due_date_label.pack()
edited_due_date_var = tk.StringVar()
edited_due_date_var.set(listbox.item(selected_item, 'values')[2])
edited_due_date_entry = tk.Entry(top, textvariable=edited_due_date_var, width=20)
edited_due_date_entry.pack()
save_changes_button = tk.Button(top, text="Save Changes", command=lambda: save_changes(task_id))
save_changes_button.pack()
def save_changes(task_id):
edited_task = edited_task_var.get()
edited_priority = edited_priority_var.get()
edited_due_date = edited_due_date_var.get()
if edited_task:
updated_task_display = f"{edited_task} - Priority: {edited_priority} - Due Date: {edited_due_date} - {listbox.item(selected_item, 'values')[3]}"
listbox.item(selected_item, values=(edited_task, edited_priority, edited_due_date, listbox.item(selected_item, 'values')[3]))
connection = sqlite3.connect("tasks.db")
cursor = connection.cursor()
cursor.execute("UPDATE tasks SET task=?, priority=?, due_date=? WHERE id=?", (edited_task, edited_priority, edited_due_date, task_id))
connection.commit()
connection.close()
top.destroy()
# Create the main window
root = tk.Tk()
root.title("Todo List App")
# Create and configure the task entry
entry = tk.Entry(root, width=30)
entry.pack(pady=10)
# Create and configure the Priority dropdown
priority_var = tk.StringVar(root)
priority_var.set("Low")
priority_label = tk.Label(root, text="Priority:")
priority_label.pack()
priority_dropdown = tk.OptionMenu(root, priority_var, "Low", "Medium", "High")
priority_dropdown.pack()
# Create and configure the Due Date entry
due_date_var = tk.StringVar(root)
due_date_label = tk.Label(root, text="Due Date:")
due_date_label.pack()
due_date_entry = tk.Entry(root, textvariable=due_date_var, width=20)
due_date_entry.pack()
# Create and configure the Add Task button
add_button = tk.Button(root, text="Add Task", command=add_task)
add_button.pack()
# Create and configure the task treeview
columns = ("Task", "Priority", "Due Date", "Status")
listbox = ttk.Treeview(root, columns=columns, show="headings", selectmode="browse")
for col in columns:
listbox.heading(col, text=col)
listbox.column(col, width=120, anchor="center")
listbox.pack(pady=10)
# Create and configure the Remove Task button
remove_button = tk.Button(root, text="Remove Task", command=remove_task)
remove_button.pack()
# Create and configure the Complete Task button
complete_button = tk.Button(root, text="Complete Task", command=complete_task)
complete_button.pack()
# Create and configure the Save Tasks button
save_button = tk.Button(root, text="Save Tasks", command=save_tasks)
save_button.pack()
# Create and configure the Load Tasks button
load_button = tk.Button(root, text="Load Tasks", command=load_tasks)
load_button.pack()
# Create and configure the Sort Tasks button
sort_button = tk.Button(root, text="Sort Tasks", command=sort_tasks)
sort_button.pack()
# Create and configure the Filter Tasks dropdown
filter_var = tk.StringVar(root)
filter_var.set("All")
filter_label = tk.Label(root, text="Filter Tasks:")
filter_label.pack()
filter_dropdown = tk.OptionMenu(root, filter_var, "All", "Completed", "Not Completed")
filter_dropdown.pack()
# Create and configure the Filter Tasks button
filter_button = tk.Button(root, text="Filter Tasks", command=filter_tasks)
filter_button.pack()
# Create and configure the Open Calendar button
calendar_button = tk.Button(root, text="Open Calendar", command=open_calendar)
calendar_button.pack()
# Create and configure the Edit Task button
edit_button = tk.Button(root, text="Edit Task", command=edit_task)
edit_button.pack()
# Load tasks from the database on startup
create_table()
load_tasks()
# Start the main event loop
root.mainloop()