def delete_task(self):
try:
task_index = self.task_list.curselection()[0]
self.task_list.delete(task_index)
self.tasks.pop(task_index)
except:
messagebox.showwarning(Error, Select a task to delete)
def save_tasks(self):
with open(tasks.txt, w) as file:
for task in self.tasks:
file.write(task + \n)
messagebox.showinfo(Tasks Saved, Tasks have been saved to tasks.txt)
def load_tasks(self):
try:
with open(tasks.txt, r) as file:
self.tasks = file.readlines()
self.tasks = [task.strip() for task in self.tasks]
self.task_list.delete(0, tk.END)
for task in self.tasks:
self.task_list.insert(tk.END, task)
except FileNotFoundError:
messagebox.showwarning(Error, No saved tasks found)
This program creates a simple todo list with the following features:
* Add tasks to the list
* Delete tasks from the list
* Save tasks to a file
* Load tasks from a file
Here's how it works:
1、 The program creates a GUI window with a list box to display tasks, an entry field to input new tasks, and buttons to add, delete, save, and load tasks.
2、 When you enter a new task and click Add Task, it gets added to the list.
3、 You can delete a task by selecting it in the list and clicking Delete Task.
4、 Clicking Save Tasks saves all tasks in the list to a file named `tasks.txt`.
5、 Clicking Load Tasks loads tasks from `tasks.txt` and displays them in the list.
This is a basic example、You can enhance it by adding more features like editing tasks, due dates, priorities, etc.