-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoList.py
89 lines (65 loc) · 2.05 KB
/
ToDoList.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
from tkinter import *
import tkinter.messagebox as msgbox
from os import remove, rename
def addtask():
task_list.insert(END, task.get())
with open("Task.txt", 'a') as f:
f.write(task.get() + '\n')
def addtaskenter(event):
addtask()
def deletetask():
selected_indices = task_list.curselection()
if selected_indices:
selected_index = selected_indices[0]
global selected_item
selected_item = task_list.get(selected_index)
task_list.delete(selected_index)
else:
msgbox.showinfo("Error", "First select a task to delete!")
return
with open("Task.txt", 'r') as f:
tk = f.readlines()
for i in tk:
if i == selected_item:
tk.remove(selected_item)
with open("Task2.txt", 'a') as f:
for i in tk:
f.write(i)
remove("Task.txt")
rename("Task2.txt", "Task.txt")
def deleteall():
task_list.delete(0, END)
remove("Task.txt")
def showtasks():
try:
with open("Task.txt", "r") as f:
tk = f.readlines()
except FileNotFoundError:
return
for i in tk:
task_list.insert(END, i)
root = Tk()
root.title("To-Do List")
root.wm_iconbitmap("todolist.ico")
root.geometry("400x300")
root.minsize(width=200, height=100)
h1 = Label(root, text="To-Do List", font="Corbel 20 bold")
h1.pack()
f1 = Frame(root)
f1.pack(side=LEFT, padx=40, pady=10)
f2 = Frame(root)
f2.pack(side=LEFT, padx=40, pady=10)
task = StringVar()
field = Entry(f1, textvariable=task)
field.pack(side=TOP)
field.bind('<Return>', addtaskenter)
add_btn = Button(f1, fg='green', command=addtask, text="Add Task", width=16)
add_btn.pack(side=TOP, pady=25)
del_btn = Button(f1, fg='red', command=deletetask, text="Delete Task", width=16)
del_btn.pack(side=TOP)
del_all_btn = Button(f1, fg='blue', command=deleteall, text="Delete All Tasks", width=16)
del_all_btn.pack(side=TOP, pady=25)
task_list = Listbox(f2)
task_list.pack()
showtasks()
root.mainloop()