Creating a simple To-Do List application can be a great project for beginners to dive into Python programming and GUI (Graphical User Interface) development. This project uses Python’s Tkinter library to create a simple and interactive UI for To-Do application.
![Todolist To Do List](https://codeblockhub.com/wp-content/uploads/2023/08/Todolist-1024x576.webp)
Requirements
- Python installed on your computer
- Basic understanding of Python programming
Steps to Build the Project
Step 1: Import Tkinter
Firstly, you need to import the Tkinter library.
import tkinter as tk
from tkinter import ttk
Step 2: Initialize the Tkinter Window
Initialize the Tkinter window and set its title and size.
root = tk.Tk()
root.title("To-Do List App")
root.geometry("400x400")
Step 3: Create the To-Do List UI Elements
Create labels, buttons, and the listbox for the To-Do list.
label = ttk.Label(root, text="Enter your task:")
label.pack(pady=10)
entry = ttk.Entry(root, width=50)
entry.pack(pady=10)
button_frame = ttk.Frame(root)
button_frame.pack(pady=20)
add_button = ttk.Button(button_frame, text="Add", command=lambda: add_task())
add_button.grid(row=0, column=0, padx=10)
delete_button = ttk.Button(button_frame, text="Delete", command=lambda: delete_task())
delete_button.grid(row=0, column=1, padx=10)
task_listbox = tk.Listbox(root, width=50, height=10, selectmode=tk.SINGLE)
task_listbox.pack()
Step 4: Add Functionality to Add and Delete Tasks
Create functions to add and delete tasks in the listbox.
def add_task():
task = entry.get()
if task != "":
task_listbox.insert(tk.END, task)
entry.delete(0, tk.END)
def delete_task():
try:
index = task_listbox.curselection()[0]
task_listbox.delete(index)
except:
pass
Step 5: Run the Tkinter Event Loop
Finally, run the Tkinter event loop to keep the application running.
root.mainloop()
Complete Code
Here’s the complete code for your To-Do List application.
import tkinter as tk
from tkinter import ttk
def add_task():
task = entry.get()
if task != "":
task_listbox.insert(tk.END, task)
entry.delete(0, tk.END)
def delete_task():
try:
index = task_listbox.curselection()[0]
task_listbox.delete(index)
except:
pass
root = tk.Tk()
root.title("To-Do List App")
root.geometry("400x400")
label = ttk.Label(root, text="Enter your task:")
label.pack(pady=10)
entry = ttk.Entry(root, width=50)
entry.pack(pady=10)
button_frame = ttk.Frame(root)
button_frame.pack(pady=20)
add_button = ttk.Button(button_frame, text="Add", command=lambda: add_task())
add_button.grid(row=0, column=0, padx=10)
delete_button = ttk.Button(button_frame, text="Delete", command=lambda: delete_task())
delete_button.grid(row=0, column=1, padx=10)
task_listbox = tk.Listbox(root, width=50, height=10, selectmode=tk.SINGLE)
task_listbox.pack()
root.mainloop()
Save this code in a Python file and run it. A window should appear with options to add and delete tasks from your To-Do List.
And there you have it! A simple but functional To-Do List application built with Python and Tkinter. Feel free to add more features, like saving tasks to a file or setting deadlines, as you become more comfortable with Python and Tkinter.