ProyectoFinalPython/app/widgets/TodoTab.py

62 lines
2.1 KiB
Python

import os
from tkinter import Frame, Entry, Button, Listbox, END, StringVar, Tk
from app.widgets.abc import ThreadedTab
class TodoTab(ThreadedTab):
def __init__(self, root: Frame | Tk, **kwargs):
self.todo_file = 'todo.list'
self.todo_items = []
self.load_todo_list()
super().__init__(root, **kwargs)
def build(self):
self.todo_frame = Frame(self)
self.todo_frame.pack(fill="both", expand=True)
self.todo_listbox = Listbox(self.todo_frame)
self.todo_listbox.pack(fill="both", expand=True, padx=5, pady=5)
self.todo_listbox.bind("<Double-Button-1>", self.remove_todo)
self.new_todo_var = StringVar()
self.new_todo_entry = Entry(self.todo_frame, textvariable=self.new_todo_var)
self.new_todo_entry.pack(fill="x", padx=5, pady=5)
self.new_todo_entry.bind("<Return>", self.add_todo)
self.add_button = Button(self.todo_frame, text="Add", command=self.add_todo)
self.add_button.pack(padx=5, pady=5)
self.update_listbox()
def load_todo_list(self):
if os.path.exists(self.todo_file):
with open(self.todo_file, 'r') as file:
self.todo_items = [line.strip() for line in file.readlines()]
def save_todo_list(self):
with open(self.todo_file, 'w') as file:
for item in self.todo_items:
file.write(f"{item}\n")
def add_todo(self, event=None):
new_todo = self.new_todo_var.get().strip()
if new_todo:
self.todo_items.append(new_todo)
self.new_todo_var.set("")
self.update_listbox()
self.save_todo_list()
def remove_todo(self, event=None):
selected_indices = self.todo_listbox.curselection()
for index in selected_indices[::-1]:
del self.todo_items[index]
self.update_listbox()
self.save_todo_list()
def update_listbox(self):
self.todo_listbox.delete(0, END)
for item in self.todo_items:
self.todo_listbox.insert(END, item)
def task(self, *args):
pass