import tkinter as tk
from tkinter import messagebox

class Note:
    def __init__(self, title, content):
        self.title = title
        self.content = content

class NoteTakingApp:
    def __init__(self, master):
        self.master = master
        self.master.title("Note-Taking Application")

        self.notes = []

        self.title_label = tk.Label(master, text="Title:")
        self.title_label.grid(row=0, column=0, sticky="w")
        self.title_entry = tk.Entry(master)
        self.title_entry.grid(row=0, column=1)

        self.content_label = tk.Label(master, text="Content:")
        self.content_label.grid(row=1, column=0, sticky="w")
        self.content_text = tk.Text(master, height=10, width=50)
        self.content_text.grid(row=1, column=1)

        self.create_button = tk.Button(master, text="Create Note", command=self.create_note)
        self.create_button.grid(row=2, column=0, columnspan=2)

        self.listbox = tk.Listbox(master)
        self.listbox.grid(row=3, column=0, columnspan=2)

        self.edit_button = tk.Button(master, text="Edit Note", command=self.edit_note)
        self.edit_button.grid(row=4, column=0, sticky="w")

        self.delete_button = tk.Button(master, text="Delete Note", command=self.delete_note)
        self.delete_button.grid(row=4, column=1, sticky="e")

        self.populate_notes()

    def create_note(self):
        title = self.title_entry.get()
        content = self.content_text.get("1.0", "end-1c")
        if title:
            self.notes.append(Note(title, content))
            self.populate_notes()
            self.title_entry.delete(0, "end")
            self.content_text.delete("1.0", "end")
        else:
            messagebox.showerror("Error", "Please enter a title for the note.")

    def populate_notes(self):
        self.listbox.delete(0, "end")
        for note in self.notes:
            self.listbox.insert("end", note.title)

    def edit_note(self):
        selection = self.listbox.curselection()
        if selection:
            index = selection[0]
            note = self.notes[index]
            self.title_entry.insert(0, note.title)
            self.content_text.insert("1.0", note.content)
            del self.notes[index]
            self.populate_notes()
        else:
            messagebox.showerror("Error", "Please select a note to edit.")

    def delete_note(self):
        selection = self.listbox.curselection()
        if selection:
            index = selection[0]
            del self.notes[index]
            self.populate_notes()
        else:
            messagebox.showerror("Error", "Please select a note to delete.")

def main():
    root = tk.Tk()
    app = NoteTakingApp(root)
    root.mainloop()

if __name__ == "__main__":
    main()
