1
+ import tkinter as tk
2
+ from tkinter import simpledialog , messagebox
3
+
4
+ def add_task ():
5
+ global listbox_tasks , entry_task
6
+ task = entry_task .get ()
7
+ if task :
8
+ listbox_tasks .insert (tk .END , task )
9
+ entry_task .delete (0 , tk .END )
10
+ else :
11
+ messagebox .showwarning ("Warning" , "Please enter a task." )
12
+
13
+ def delete_task ():
14
+ global listbox_tasks
15
+ try :
16
+ index = listbox_tasks .curselection ()[0 ]
17
+ listbox_tasks .delete (index )
18
+ except IndexError :
19
+ messagebox .showwarning ("Warning" , "Please select a task to delete." )
20
+
21
+ def edit_task ():
22
+ global listbox_tasks
23
+ try :
24
+ index = listbox_tasks .curselection ()[0 ]
25
+ old_task = listbox_tasks .get (index )
26
+ new_task = simpledialog .askstring ("Edit Task" , "Edit the selected task:" , initialvalue = old_task )
27
+ if new_task :
28
+ listbox_tasks .delete (index )
29
+ listbox_tasks .insert (index , new_task )
30
+ except IndexError :
31
+ messagebox .showwarning ("Warning" , "Please select a task to edit." )
32
+
33
+ def main ():
34
+ global listbox_tasks , entry_task
35
+ root = tk .Tk ()
36
+ root .title ("Task 1 To-Do List" )
37
+
38
+ frame_tasks = tk .Frame (root )
39
+ frame_tasks .pack (pady = 10 )
40
+
41
+ listbox_tasks = tk .Listbox (frame_tasks , width = 50 , height = 10 , selectbackground = "green" )
42
+ listbox_tasks .pack (side = tk .LEFT , fill = tk .BOTH )
43
+
44
+ scrollbar_tasks = tk .Scrollbar (frame_tasks )
45
+ scrollbar_tasks .pack (side = tk .RIGHT , fill = tk .BOTH )
46
+
47
+ listbox_tasks .config (yscrollcommand = scrollbar_tasks .set )
48
+ scrollbar_tasks .config (command = listbox_tasks .yview )
49
+
50
+ entry_task = tk .Entry (root , width = 50 )
51
+ entry_task .pack (pady = 10 )
52
+
53
+ button_add_task = tk .Button (root , text = "Add Task" , command = add_task )
54
+ button_add_task .pack (side = tk .LEFT , padx = 5 )
55
+
56
+ button_delete_task = tk .Button (root , text = "Delete Task" , command = delete_task )
57
+ button_delete_task .pack (side = tk .LEFT , padx = 5 )
58
+
59
+ button_edit_task = tk .Button (root , text = "Edit Task" , command = edit_task )
60
+ button_edit_task .pack (side = tk .LEFT , padx = 5 )
61
+
62
+ root .mainloop ()
63
+
64
+ if __name__ == "__main__" :
65
+ main ()
0 commit comments