-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo_list.py
More file actions
143 lines (125 loc) · 2.87 KB
/
Copy pathtodo_list.py
File metadata and controls
143 lines (125 loc) · 2.87 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
import tkinter as tk
from tkinter import messagebox
FILE_NAME = "tasks.txt"
# --- FILE LOGIC ---
def load_tasks():
try:
file = open(FILE_NAME, "r")
tasks = file.read().splitlines()
file.close()
return tasks
except FileNotFoundError:
return []
def save_tasks(tasks):
file = open(FILE_NAME, "w")
for task in tasks:
file.write(task + "\n")
file.close()
# --- GUI ---
tasks = load_tasks()
def refresh_tasks():
listbox.delete(0, tk.END)
for i, task in enumerate(tasks, start=1):
listbox.insert(tk.END, f"{i}. {task}")
def add_task():
task = entry.get().strip()
if task == "":
messagebox.showwarning("Warning", "Task cannot be empty")
return
tasks.append(task)
save_tasks(tasks)
entry.delete(0, tk.END)
refresh_tasks()
def delete_task():
try:
index = listbox.curselection()[0]
tasks.pop(index)
save_tasks(tasks)
refresh_tasks()
except IndexError:
messagebox.showwarning("Warning", "Please select a task to delete")
def update_task():
try:
index = listbox.curselection()[0]
new_task = entry.get().strip()
if new_task == "":
messagebox.showwarning("Warning", "Task cannot be empty")
return
tasks[index] = new_task
save_tasks(tasks)
entry.delete(0, tk.END)
refresh_tasks()
except IndexError:
messagebox.showwarning("Warning", "Please select a task to update")
# --- WINDOW ---
root = tk.Tk()
root.title("To-Do List")
root.geometry("400x500")
root.configure(bg="#f4f6f8")
root.resizable(False, False)
# --- TITLE ---
title = tk.Label(
root,
text="To-Do List",
font=("Arial", 18, "bold"),
bg="#f4f6f8",
fg="#2c3e50"
)
title.pack(pady=15)
# ---INPUT ---
entry = tk.Entry(
root,
width=30,
font=("Arial", 12),
bg="#ffffff",
fg="#000000",
relief="solid"
)
entry.pack(pady=10)
# --- BUTTONS ---
btn_frame = tk.Frame(root, bg="#f4f6f8")
btn_frame.pack(pady=10)
add_btn = tk.Button(
btn_frame,
text="Add Task",
width=12,
bg="#2ecc71",
fg="white",
font=("Arial", 10, "bold"),
command=add_task
)
add_btn.grid(row=0, column=0, padx=10)
delete_btn = tk.Button(
btn_frame,
text="Delete Task",
width=12,
bg="#e74c3c",
fg="white",
font=("Arial", 10, "bold"),
command=delete_task
)
delete_btn.grid(row=0, column=1, padx=10)
ubt_btn = tk.Button(
btn_frame,
text="Update Task",
width=12,
bg="#3498db",
fg="white",
font=("Arial", 10, "bold"),
command=update_task
)
ubt_btn.grid(row=0, column=2, padx=10)
# --- TASK LIST ---
listbox = tk.Listbox(
root,
width=40,
height=15,
font=("Arial", 11),
bg="#ffffff",
fg="#34495e",
selectbackground="#0b68a6",
relief="solid"
)
listbox.pack(pady=15)
refresh_tasks()
root.mainloop()