99 lines
4.1 KiB
Python
99 lines
4.1 KiB
Python
import tkinter as tk
|
||
from tkinter import ttk
|
||
import threading
|
||
|
||
def crear_solapa_todo(tab):
|
||
"""Crea una pestaña de To-Do List que usa TODO el espacio disponible."""
|
||
|
||
tareas = []
|
||
|
||
# Configurar el frame principal para que use todo el espacio
|
||
frame_principal = ttk.Frame(tab, padding=10)
|
||
frame_principal.grid(row=0, column=0, sticky="nsew")
|
||
|
||
tab.columnconfigure(0, weight=1)
|
||
tab.rowconfigure(0, weight=1)
|
||
frame_principal.columnconfigure(0, weight=1)
|
||
frame_principal.rowconfigure(1, weight=1) # La lista de tareas ocupa el mayor espacio
|
||
|
||
# Estilos para botones de colores
|
||
style = ttk.Style()
|
||
style.configure("BotonAzul.TButton", font=("Arial", 12, "bold"), foreground="#007BFF", background="#007BFF")
|
||
style.configure("BotonRojo.TButton", font=("Arial", 12, "bold"), foreground="#DC3545", background="#DC3545")
|
||
style.configure("BotonVerde.TButton", font=("Arial", 12, "bold"), foreground="#28A745", background="#28A745")
|
||
|
||
# Entrada para agregar nuevas tareas
|
||
entry = ttk.Entry(frame_principal, font=("Arial", 14))
|
||
entry.grid(row=0, column=0, padx=5, pady=5, sticky="ew")
|
||
|
||
# Marco para la lista con scroll
|
||
frame_lista = ttk.Frame(frame_principal)
|
||
frame_lista.grid(row=1, column=0, pady=5, sticky="nsew")
|
||
|
||
frame_lista.columnconfigure(0, weight=1)
|
||
frame_lista.rowconfigure(0, weight=1)
|
||
|
||
# ListBox con scrollbar
|
||
todo_listbox = tk.Listbox(frame_lista, font=("Arial", 14), selectbackground="#00A8E8")
|
||
todo_listbox.grid(row=0, column=0, sticky="nsew")
|
||
|
||
scrollbar = ttk.Scrollbar(frame_lista, orient="vertical", command=todo_listbox.yview)
|
||
scrollbar.grid(row=0, column=1, sticky="ns")
|
||
todo_listbox.config(yscrollcommand=scrollbar.set)
|
||
|
||
# Marco inferior para botones
|
||
frame_botones = ttk.Frame(frame_principal)
|
||
frame_botones.grid(row=2, column=0, pady=5, sticky="ew")
|
||
frame_botones.columnconfigure((0,1,2), weight=1)
|
||
|
||
# Botones
|
||
boton_agregar = ttk.Button(frame_botones, text="➕ Agregar", style="BotonAzul.TButton", command=lambda: agregar_tarea(entry, todo_listbox, tareas))
|
||
boton_agregar.grid(row=0, column=0, padx=5, sticky="ew")
|
||
|
||
boton_eliminar = ttk.Button(frame_botones, text="🗑️ Eliminar", style="BotonRojo.TButton", command=lambda: eliminar_tarea(todo_listbox, tareas))
|
||
boton_eliminar.grid(row=0, column=1, padx=5, sticky="ew")
|
||
|
||
boton_marcar = ttk.Button(frame_botones, text="✔️ Marcar", style="BotonVerde.TButton", command=lambda: marcar_tarea(todo_listbox, tareas))
|
||
boton_marcar.grid(row=0, column=2, padx=5, sticky="ew")
|
||
|
||
# Inicializar la lista vacía
|
||
actualizar_todo_list(todo_listbox, tareas)
|
||
|
||
def actualizar_todo_list(todo_listbox, tareas):
|
||
"""Actualizar la lista de tareas en la interfaz."""
|
||
todo_listbox.delete(0, tk.END)
|
||
for tarea, completada in tareas:
|
||
estado = "✅" if completada else "⬜"
|
||
todo_listbox.insert(tk.END, f"{estado} {tarea}")
|
||
|
||
def agregar_tarea(entry, todo_listbox, tareas):
|
||
"""Agregar una nueva tarea a la lista en un hilo separado."""
|
||
def tarea_hilo():
|
||
nueva_tarea = entry.get()
|
||
if nueva_tarea.strip():
|
||
tareas.append((nueva_tarea, False)) # Agregar como tarea no completada
|
||
todo_listbox.after(0, actualizar_todo_list, todo_listbox, tareas)
|
||
entry.delete(0, tk.END)
|
||
threading.Thread(target=tarea_hilo).start()
|
||
|
||
def eliminar_tarea(todo_listbox, tareas):
|
||
"""Eliminar una tarea de la lista."""
|
||
def tarea_hilo():
|
||
seleccion = todo_listbox.curselection()
|
||
if seleccion:
|
||
indice = seleccion[0]
|
||
tareas.pop(indice)
|
||
todo_listbox.after(0, actualizar_todo_list, todo_listbox, tareas)
|
||
threading.Thread(target=tarea_hilo).start()
|
||
|
||
def marcar_tarea(todo_listbox, tareas):
|
||
"""Marcar la tarea seleccionada como completada o no."""
|
||
def tarea_hilo():
|
||
seleccion = todo_listbox.curselection()
|
||
if seleccion:
|
||
indice = seleccion[0]
|
||
tarea, completada = tareas[indice]
|
||
tareas[indice] = (tarea, not completada) # Alternar estado
|
||
todo_listbox.after(0, actualizar_todo_list, todo_listbox, tareas)
|
||
threading.Thread(target=tarea_hilo).start()
|