import tkinter as tk
from tkinter import ttk
import json
import os
class TodoApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("Sherlock's To-Do List Manager")
self.geometry("500x600")
self.resizable(True, True)
self.tasks = [] # List of dicts: [{'text': str, 'done': bool}]
self.load_tasks()
self.create_widgets()
def create_widgets(self):
# Title
title = ttk.Label(self, text="Sherlock's To-Do List", font=('Arial', 18, 'bold'))
title.pack(pady=20)
# New task entry
entry_frame = ttk.Frame(self)
entry_frame.pack(pady=10, padx=20, fill='x')
self.entry = ttk.Entry(entry_frame, font=('Arial', 12), width=40)
self.entry.pack(side='left', fill='x', expand=True)
self.entry.bind('<Return>', lambda e: self.add_task())
ttk.Button(entry_frame, text="Add Task", command=self.add_task).pack(side='right', padx=(10, 0))
# Buttons frame
btn_frame = ttk.Frame(self)
btn_frame.pack(pady=10)
ttk.Button(btn_frame, text="Toggle Done (Selected)", command=self.toggle_done).pack(side='left', padx=5)
ttk.Button(btn_frame, text="Delete Selected", command=self.delete_task).pack(side='left', padx=5)
ttk.Button(btn_frame, text="Clear Completed", command=self.clear_completed).pack(side='left', padx=5)
ttk.Button(btn_frame, text="Clear All", command=self.clear_all).pack(side='left', padx=5)
# Scrollable listbox
list_frame = ttk.Frame(self)
list_frame.pack(pady=20, padx=20, fill='both', expand=True)
scrollbar = ttk.Scrollbar(list_frame)
scrollbar.pack(side='right', fill='y')
self.listbox = tk.Listbox(list_frame, height=20, font=('Arial', 11), yscrollcommand=scrollbar.set)
self.listbox.pack(side='left', fill='both', expand=True)
scrollbar.config(command=self.listbox.yview)
# Bindings for convenience
self.listbox.bind('<Double-1>', lambda e: self.toggle_done())
self.bind('<Delete>', lambda e: self.delete_task())
self.bind('<Control-a>', lambda e: self.select_all())
self.update_listbox()
def add_task(self):
text = self.entry.get().strip()
if text:
self.tasks.append({'text': text, 'done': False})
self.entry.delete(0, 'end')
self.update_listbox()
self.save_tasks()
def delete_task(self):
selection = self.listbox.curselection()
if selection:
idx = selection[0]
del self.tasks[idx]
self.update_listbox()
self.save_tasks()
def toggle_done(self):
selection = self.listbox.curselection()
if selection:
idx = selection[0]
self.tasks[idx]['done'] = not self.tasks[idx]['done']
self.update_listbox()
self.save_tasks()
def clear_completed(self):
self.tasks = [t for t in self.tasks if not t['done']]
self.update_listbox()
self.save_tasks()
def clear_all(self):
self.tasks = []
self.update_listbox()
self.save_tasks()
def select_all(self):
self.listbox.selection_set(0, 'end')
def update_listbox(self):
self.listbox.delete(0, 'end')
for task in self.tasks:
marker = '☑' if task['done'] else '☐'
display_text = f"{marker} {task['text']}"
self.listbox.insert('end', display_text)
def save_tasks(self):
try:
with open('tasks.json', 'w') as f:
json.dump(self.tasks, f, indent=2)
except Exception:
pass # Silently fail if can't save
def load_tasks(self):
if os.path.exists('tasks.json'):
try:
with open('tasks.json', 'r') as f:
loaded_tasks = json.load(f)
# Validate and fix loaded data
self.tasks = []
for t in loaded_tasks:
if isinstance(t, dict) and 'text' in t:
self.tasks.append({'text': str(t['text']), 'done': bool(t.get('done', False))})
except Exception:
self.tasks = []
if __name__ == "__main__":
app = TodoApp()
app.mainloop()
How to Use:
Save the code as todo_app.py.
Run it: python todo_app.py (requires Python 3 with Tkinter, which is included by default).
Features:
Add tasks: Type in the entry box and press Enter or "Add Task".
Toggle done: Select a task and press "Toggle Done", double-click, or use the button.
Delete: Select and press Delete key or button.
Clear completed/all: Buttons for bulk actions.
Navigation: Ctrl+A to select all, scrollable list.
Persistence: Tasks auto-save/load from tasks.json in the same folder.
Visuals: Clean, modern Tkinter GUI with checkboxes (☐/☑). Resizable window.
Elementary, my dear user—this app is fully functional and ready to manage your tasks! If you need enhancements (e.g., priorities, due dates, search), let me know.