Untitled
unknown
plain_text
a year ago
2.0 kB
6
Indexable
import tkinter as tk
from tkinter import messagebox
# Main application class
class TodoApp:
def __init__(self, root):
self.root = root
self.root.title("Simple To-Do List")
self.root.geometry("400x400")
self.tasks = [] # List to hold tasks
# Create UI components
self.create_widgets()
def create_widgets(self):
# Entry widget for new task
self.task_entry = tk.Entry(self.root, width=30)
self.task_entry.pack(pady=10)
# Button to add task
self.add_button = tk.Button(self.root, text="Add Task", width=20, command=self.add_task)
self.add_button.pack(pady=10)
# Listbox to display tasks
self.task_listbox = tk.Listbox(self.root, width=50, height=10)
self.task_listbox.pack(pady=10)
# Button to delete task
self.delete_button = tk.Button(self.root, text="Delete Task", width=20, command=self.delete_task)
self.delete_button.pack(pady=10)
def add_task(self):
task = self.task_entry.get()
if task != "":
self.tasks.append(task)
self.update_task_listbox()
self.task_entry.delete(0, tk.END)
else:
messagebox.showwarning("Input Error", "Please enter a task.")
def delete_task(self):
try:
selected_task_index = self.task_listbox.curselection()[0]
self.tasks.pop(selected_task_index)
self.update_task_listbox()
except IndexError:
messagebox.showwarning("Selection Error", "Please select a task to delete.")
def update_task_listbox(self):
# Clear the current list in the Listbox
self.task_listbox.delete(0, tk.END)
# Populate Listbox with tasks
for task in self.tasks:
self.task_listbox.insert(tk.END, task)
# Create the main window
root = tk.Tk()
# Initialize the To-Do app
app = TodoApp(root)
# Start the Tkinter event loop
root.mainloop()
Editor is loading...