How To Build A Simple To-Do List App in Python

How To Build A Simple To-Do List App in Python

A to-do list app is a simple but essential tool for managing daily tasks and staying organized. 

In this article, we will show you how to build a simple to-do list app in Python using the Tkinter GUI toolkit.

Simple To-Do List App

The app will have the following features:

  • A user-friendly interface for adding and deleting tasks
  • A list view to display all tasks
  • A save button to save tasks to a text file

Let's get started!

Step 1: 

Install Required Libraries To begin, we need to install the Tkinter library. Tkinter is a built-in Python library for creating GUI applications.

To install Tkinter, open your terminal or command prompt and run the following command:

pip install tkinter

Step 2: 

Create the GUI Now that we have installed Tkinter, let’s create the GUI for our to-do list app.

Create a new Python file and import the Tkinter library:

from tkinter import *

Next, create a new window using the Tk() function:

root = Tk()

This creates a new window with a default size and title.

Now, let’s add a label to the window to display the app name:

app_name = Label(root, text="To-Do List App")
app_name.pack()

This creates a new label widget with the text “To-Do List App” and adds it to the window using the pack() method.

Next, let’s add an input field and a button to allow users to add tasks:

input_frame = Frame(root)
input_frame.pack()

input_field = Entry(input_frame)
input_field.pack(side=LEFT)

add_button = Button(input_frame, text="Add")
add_button.pack(side=LEFT)

This creates a new frame to hold the input field and button, adds an input field widget to the frame, and adds a button widget to the frame with the text “Add”.

Finally, let’s add a list box widget to display all tasks:

task_list = Listbox(root)
task_list.pack()

This creates a new list box widget and adds it to the window.

Step 3: 

Add Functionality Now that we have created the GUI, let’s add some functionality to the app.

First, let’s create a function to add tasks to the list box:

def add_task():
task = input_field.get()
task_list.insert(END, task)
input_field.delete(0, END)

This function gets the text entered in the input field, adds it to the end of the list box using the insert() method, and clears the input field using the delete() method.

Next, let’s bind the add_task() function to the add button:

add_button.config(command=add_task)

This binds the add_task() function to the add button so that it is executed when the button is clicked.

Now, let’s create a function to delete tasks from the list box:

def delete_task():
task_index = task_list.curselection()
task_list.delete(task_index)

This function gets the index of the currently selected task using the curselection() method, and deletes the task from the list box using the delete() method.

Finally, let’s add a delete button to the window and bind it to the delete_task() function:

delete_button = Button(root, text="Delete", command=delete_task)
delete_button.pack()

This creates a new button widget with the text “Delete” and binds it to the delete_task() function using the command attribute.

Step 4: 

Save Tasks Now that we have added functionality to the app, let’s add a save button to save tasks to a text file.

First, let’s create a function to save tasks:

def save_tasks():
tasks
= task_list.get(0, END)
with open("tasks.txt", "w") as f:
for task in tasks:
f.write(task + "\n")

This function gets all tasks in the list box using the get() method, and writes each task to a text file named “tasks.txt” using a for loop and the write() method.

Next, let’s add a save button to the window and bind it to the save_tasks() function:

save_button = Button(root, text="Save", command=save_tasks)
save_button.pack()

This creates a new button widget with the text “Save” and binds it to the save_tasks() function using the command attribute.

Step 5: 

Run the App Now that we have completed the app, let’s run it!

Add the following code to the end of the Python file:

root.mainloop()

This starts the main event loop for the app, allowing it to respond to user events.

Save the file and run it in your terminal or command prompt using the following command:

python app.py

You should see a new window appear with the app name, an input field, an add button, a list box, a delete button, and a save button.

You can now add tasks using the input field and add button, delete tasks using the delete button, and save tasks to a text file using the save button.

Congratulations! You have successfully built a simple to-do list app in Python using the Tkinter GUI toolkit. 

With a little more time and effort, you can add more features to the app, such as due dates and reminders, to make it even more useful.

Post a Comment

Previous Post Next Post

Ad 1

Ad 2