import tkinter as tk
from tkinter import messagebox
import json

# Load tasks from a file
def load_tasks():
    try:
        with open('tasks.json', 'r') as file:
            return json.load(file)
    except FileNotFoundError:
        return []

# Save tasks to a file
def save_tasks(tasks):
    with open('tasks.json', 'w') as file:
        json.dump(tasks, file)

# Add a task
def add_task():
    task = task_entry.get()
    if task != "":
        tasks.append(task)
        save_tasks(tasks)
        update_listbox()
    else:
        messagebox.showinfo("Warning", "You must enter a task.")
    task_entry.delete(0, "end")

# Delete a task
def delete_task():
    task = listbox.get("active")
    if task in tasks:
        tasks.remove(task)
        save_tasks(tasks)
    update_listbox()

# Update the listbox
def update_listbox():
    clear_listbox()
    for task in tasks:
        listbox.insert("end", task)

# Clear the listbox
def clear_listbox():
    listbox.delete(0, "end")

tasks = load_tasks()

window = tk.Tk()
window.title("To-Do List")

frame = tk.Frame(window)
frame.pack()

listbox = tk.Listbox(frame, height=15, width=50)
listbox.pack(side="left")

scrollbar = tk.Scrollbar(frame)
scrollbar.pack(side="right", fill="y")

listbox.configure(yscrollcommand=scrollbar.set)
scrollbar.configure(command=listbox.yview)

task_entry = tk.Entry(window)
task_entry.pack()

add_button = tk.Button(window, text="Add task", command=add_task)
add_button.pack()

delete_button = tk.Button(window, text="Delete task", command=delete_task)
delete_button.pack()

update_listbox()

window.mainloop()
