import tkinter as tk

root = tk.Tk()
root.title("To-Do List")

# Listbox to display the tasks
tasks_listbox = tk.Listbox(root, height=10, width=40, selectmode=tk.SINGLE)
tasks_listbox.pack(padx=20, pady=20)

# Entry widget for adding/editing tasks
entry = tk.Entry(root, width=40)
entry.pack(pady=5)

# Function to add a task
def add_task():
    task = entry.get()
    if task:
        tasks_listbox.insert(tk.END, task)  # Adds the task to the end of the list
        entry.delete(0, tk.END)  # Clear the input field after adding

# Function to remove a selected task
def remove_task():
    selected_task = tasks_listbox.curselection()  # Get selected task
    if selected_task:
        tasks_listbox.delete(selected_task)  # Removes the selected task from the list

# Function to edit a selected task
def edit_task():
    selected_task = tasks_listbox.curselection()  # Get selected task
    if selected_task:
        task = entry.get()
        if task:
            tasks_listbox.delete(selected_task)  # Remove the selected task
            tasks_listbox.insert(selected_task, task)  # Insert the new task at the same position
            entry.delete(0, tk.END)  # Clear the input field after editing

# Add task button
add_button = tk.Button(root, text="Add Task", width=15, command=add_task)
add_button.pack(pady=5)

# Remove task button
remove_button = tk.Button(root, text="Remove Task", width=15, command=remove_task)
remove_button.pack(pady=5)

# Edit task button
edit_button = tk.Button(root, text="Edit Task", width=15, command=edit_task)
edit_button.pack(pady=5)

# Run the Tkinter main loop
root.mainloop()
