Home ยป To-Do list application using Python and Tkinter

To-Do list application using Python and Tkinter

A To-do list application is a very good beginners project, lets get this started for Python/

To build a simple To-Do list application using Python and Tkinter, we’ll follow these 5 simple steps:

  1. Set up the main window: Create the main application window using Tkinter.
  2. Add a listbox to display tasks: Use a Listbox widget to display the tasks.
  3. Add entry widgets for adding tasks: Use an Entry widget to input new tasks.
  4. Add buttons for adding and deleting tasks: Use Button widgets to add and delete tasks from the list.
  5. Implement functionality for the buttons: Write functions to handle adding and deleting tasks.

Let’s get started with the step by step implementation:

Step 1: Set Up the Main Window

First, we will set up the main window for the application.

import tkinter as tk
from tkinter import messagebox

# Initialize the main window
root = tk.Tk()
root.title("To-Do List Application")
root.geometry("400x400")

Step 2: Add a Listbox to Display Tasks

We’ll add a Listbox widget to display the list of tasks.

# Create a Listbox to display tasks
task_listbox = tk.Listbox(root, width=50, height=15)
task_listbox.pack(pady=20)

Step 3: Add Entry Widgets for Adding Tasks

Next, we’ll add an Entry widget for the user to input new tasks.

# Create an Entry widget for adding tasks
task_entry = tk.Entry(root, width=50)
task_entry.pack(pady=10)

Step 4: Add Buttons for Adding and Deleting Tasks

We’ll add buttons for adding and deleting tasks.

# Create a frame for the buttons
button_frame = tk.Frame(root)
button_frame.pack(pady=20)

# Create Add Task button
add_task_button = tk.Button(button_frame, text="Add Task", command=lambda: add_task())
add_task_button.pack(side=tk.LEFT, padx=10)

# Create Delete Task button
delete_task_button = tk.Button(button_frame, text="Delete Task", command=lambda: delete_task())
delete_task_button.pack(side=tk.LEFT, padx=10)

Step 5: Implement Functionality for the Buttons

Now, we will implement the functions to handle adding and deleting tasks.

# Function to add a task
def add_task():
    task = task_entry.get()
    if task != "":
        task_listbox.insert(tk.END, task)
        task_entry.delete(0, tk.END)
    else:
        messagebox.showwarning("Warning", "You must enter a task.")

# Function to delete a selected task
def delete_task():
    try:
        selected_task_index = task_listbox.curselection()[0]
        task_listbox.delete(selected_task_index)
    except IndexError:
        messagebox.showwarning("Warning", "You must select a task to delete.")

Complete Code

Here is the complete code for the To-Do list application:

import tkinter as tk
from tkinter import messagebox

# Function to add a task
def add_task():
    task = task_entry.get()
    if task != "":
        task_listbox.insert(tk.END, task)
        task_entry.delete(0, tk.END)
    else:
        messagebox.showwarning("Warning", "You must enter a task.")

# Function to delete a selected task
def delete_task():
    try:
        selected_task_index = task_listbox.curselection()[0]
        task_listbox.delete(selected_task_index)
    except IndexError:
        messagebox.showwarning("Warning", "You must select a task to delete.")

# Initialize the main window
root = tk.Tk()
root.title("To-Do List Application")
root.geometry("400x400")

# Create a Listbox to display tasks
task_listbox = tk.Listbox(root, width=50, height=15)
task_listbox.pack(pady=20)

# Create an Entry widget for adding tasks
task_entry = tk.Entry(root, width=50)
task_entry.pack(pady=10)

# Create a frame for the buttons
button_frame = tk.Frame(root)
button_frame.pack(pady=20)

# Create Add Task button
add_task_button = tk.Button(button_frame, text="Add Task", command=add_task)
add_task_button.pack(side=tk.LEFT, padx=10)

# Create Delete Task button
delete_task_button = tk.Button(button_frame, text="Delete Task", command=delete_task)
delete_task_button.pack(side=tk.LEFT, padx=10)

# Run the application
root.mainloop()

This code sets up a basic To-Do list application with functionalities to add and delete tasks.

You can run this script in your Python environment to see the application in action.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More