|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import messagebox, font |
| 3 | + |
| 4 | +# Initialize the main app window |
| 5 | +app = tk.Tk() |
| 6 | +app.title("To-Do List Notebook") |
| 7 | +app.geometry("450x500") |
| 8 | +app.config(bg="#f0f4f8") |
| 9 | + |
| 10 | +# Set custom fonts and colors |
| 11 | +header_font = font.Font(family="Helvetica", size=18, weight="bold") |
| 12 | +task_font = font.Font(family="Arial", size=12) |
| 13 | +button_color = "#4CAF50" |
| 14 | +button_hover_color = "#45a049" |
| 15 | +task_complete_color = "#2e8b57" |
| 16 | +notebook_color = "#FFF8DC" # Light beige to mimic a notebook page |
| 17 | + |
| 18 | +# Task list to hold tasks |
| 19 | +todo_list = [] |
| 20 | + |
| 21 | +# Functions to manage tasks |
| 22 | +def add_task(): |
| 23 | + """Add a new task with a checkbox to mark as completed.""" |
| 24 | + task_text = entry_task.get() |
| 25 | + if task_text: |
| 26 | + task_var = tk.BooleanVar() |
| 27 | + task_frame = tk.Frame(task_list_frame, bg=notebook_color, padx=5, pady=3) |
| 28 | + |
| 29 | + checkbox = tk.Checkbutton( |
| 30 | + task_frame, |
| 31 | + text=task_text, |
| 32 | + variable=task_var, |
| 33 | + command=lambda: toggle_complete(task_var, checkbox), |
| 34 | + font=task_font, |
| 35 | + bg=notebook_color, |
| 36 | + fg="black", |
| 37 | + selectcolor="white", |
| 38 | + activeforeground=button_color |
| 39 | + ) |
| 40 | + |
| 41 | + checkbox.pack(anchor="w", pady=2) |
| 42 | + todo_list.append({"task": task_text, "completed": task_var, "frame": task_frame}) |
| 43 | + task_frame.pack(anchor="w", padx=10, pady=5, fill="x") |
| 44 | + entry_task.delete(0, tk.END) |
| 45 | + else: |
| 46 | + messagebox.showwarning("Input Error", "Please enter a task!") |
| 47 | + |
| 48 | +def toggle_complete(task_var, checkbox): |
| 49 | + """Update the checkbox color when the task is marked complete or incomplete.""" |
| 50 | + if task_var.get(): |
| 51 | + checkbox.config(fg=task_complete_color, font=task_font) |
| 52 | + else: |
| 53 | + checkbox.config(fg="black", font=task_font) |
| 54 | + |
| 55 | +def delete_completed_tasks(): |
| 56 | + """Delete only ticked tasks from the list.""" |
| 57 | + global todo_list |
| 58 | + for task in todo_list[:]: |
| 59 | + if task["completed"].get(): |
| 60 | + task["frame"].destroy() |
| 61 | + todo_list.remove(task) |
| 62 | + update_task_list() |
| 63 | + |
| 64 | +def update_task_list(): |
| 65 | + """Refresh the display of tasks.""" |
| 66 | + for widget in task_list_frame.winfo_children(): |
| 67 | + widget.destroy() |
| 68 | + for task in todo_list: |
| 69 | + task["frame"].pack(anchor="w", padx=10, pady=5, fill="x") |
| 70 | + |
| 71 | +def on_hover(button, color): |
| 72 | + """Change button color on hover.""" |
| 73 | + button.config(bg=color) |
| 74 | + |
| 75 | +# Header |
| 76 | +header = tk.Label(app, text="My Notebook To-Do List", font=header_font, bg="#f0f4f8", fg=button_color) |
| 77 | +header.pack(pady=10) |
| 78 | + |
| 79 | +# Notebook look-alike frame for tasks with lines |
| 80 | +task_list_frame = tk.Canvas(app, bg=notebook_color, bd=0, highlightthickness=0) |
| 81 | +task_list_frame.pack(expand=True, fill="both", padx=15, pady=10) |
| 82 | + |
| 83 | +# Draw lines to mimic notebook paper |
| 84 | +for i in range(0, 500, 20): |
| 85 | + task_list_frame.create_line(10, i, 440, i, fill="#d3d3d3") |
| 86 | + |
| 87 | +# Frame for entry, add, and delete buttons |
| 88 | +frame_task = tk.Frame(app, bg="#f0f4f8") |
| 89 | +frame_task.pack(pady=15) |
| 90 | + |
| 91 | +# Entry for new tasks |
| 92 | +entry_task = tk.Entry(frame_task, width=30, font=task_font, relief="solid", bd=1) |
| 93 | +entry_task.pack(side=tk.LEFT, padx=10) |
| 94 | + |
| 95 | +# "+" button for adding tasks |
| 96 | +button_add_task = tk.Button(frame_task, text="+", command=add_task, font=("Arial", 20, "bold"), bg=button_color, fg="white", |
| 97 | + relief="flat", width=3, height=1) |
| 98 | +button_add_task.pack(side=tk.LEFT) |
| 99 | +button_add_task.bind("<Enter>", lambda e: on_hover(button_add_task, button_hover_color)) |
| 100 | +button_add_task.bind("<Leave>", lambda e: on_hover(button_add_task, button_color)) |
| 101 | + |
| 102 | +# Delete button for deleting only completed tasks |
| 103 | +button_delete_task = tk.Button(app, text="Delete completed task", command=delete_completed_tasks, font=task_font, |
| 104 | + bg=button_color, fg="white", relief="flat", padx=10, pady=5) |
| 105 | +button_delete_task.pack(pady=10) |
| 106 | +button_delete_task.bind("<Enter>", lambda e: on_hover(button_delete_task, button_hover_color)) |
| 107 | +button_delete_task.bind("<Leave>", lambda e: on_hover(button_delete_task, button_color)) |
| 108 | + |
| 109 | +# Run the main loop |
| 110 | +app.mainloop() |
0 commit comments