Site icon TechVidvan

Create a To Do List in Python

In this Python project, we will build a GUI-based To Do List using the Tkinter module and File I/O. It is a beginner-level python project, and you should have basic knowledge of file I/O and Tkinter module before you begin and you will be able to apply them in real life. Let’s get started!

About To-Do Lists

To-Do Lists are actually a list of your daily tasks that you keep on your person to remind yourself of the tasks you have to accomplish that day. In this project, we are going to make a GUI based python to-do list with options to add and delete items in your list.

Python To Do List Project

The objective of this python project is to create a To-Do List. To build this, you will need a little understanding of File I/O and the Tkinter module.

Project Prerequisites:

To build python to-do list project, we will only need the Tkinter module to create the GUI, which comes pre-installed with Python.

Download To Do Python Project Code

Please download the source code of python to-do list Project: To Do List Python Code

To Do List Python Project File Structure:

The files required in to-do list project are:

Here are the steps you will need to execute to build this python project:

  1. Importing all the necessary libraries
  2. Initializing the window and placing all the components in it
  3. Creating the add_item and delete_item functions

Let’s take a closer look at these steps:

1. Importing the library:

# Importing all the necessary modules
from tkinter import *

2. Initializing the window and placing all the components in it:

# Initializing the python to do list GUI window
root = Tk()
root.title('TechVidvan To-Do List')
root.geometry('300x400')
root.resizable(0, 0)
root.config(bg="PaleVioletRed")

# Heading Label
Label(root, text='TechVidvan Python To Do List', bg='PaleVioletRed', font=("Comic Sans MS", 15), wraplength=300).place(x=35, y=0)

# Listbox with all the tasks with a Scrollbar
tasks = Listbox(root, selectbackground='Gold', bg='Silver', font=('Helvetica', 12), height=12, width=25)

scroller = Scrollbar(root, orient=VERTICAL, command=tasks.yview)
scroller.place(x=260, y=50, height=232)

tasks.config(yscrollcommand=scroller.set)

tasks.place(x=35, y=50)

# Adding items to the Listbox
with open('tasks.txt', 'r+') as tasks_list:
    for task in tasks_list:
        tasks.insert(END, task)
    tasks_list.close()

# Creating the Entry widget where the user can enter a new item
new_item_entry = Entry(root, width=37)
new_item_entry.place(x=35, y=310)

# Creating the Buttons
add_btn = Button(root, text='Add Item', bg='Azure', width=10, font=('Helvetica', 12),
                 command=lambda: add_item(new_item_entry, tasks))
add_btn.place(x=45, y=350)

delete_btn = Button(root, text='Delete Item', bg='Azure', width=10, font=('Helvetica', 12),
                 command=lambda: delete_item(tasks))
delete_btn.place(x=150, y=350)

# Finalizing the window
root.update()
root.mainloop()

Explanation:

3. Creating the add_item and delete_item functions:

# Adding and Deleting items functions
def add_item(entry: Entry, listbox: Listbox):
    new_task = entry.get()

    listbox.insert(END, new_task)

    with open('tasks.txt', 'a') as tasks_list_file:
        tasks_list_file.write(f'\n{new_task}')


def delete_item(listbox: Listbox):
    listbox.delete(ACTIVE)

    with open('tasks.txt', 'r+') as tasks_list_file:
        lines = tasks_list_file.readlines()

        tasks_list_file.truncate()

        for line in lines:
            if listbox.get(ACTIVE) == line[:-2]:
                lines.remove(line)
            tasks_list_file.write(line)

        tasks_list_file.close()

Explanation:

Python To-do List Output

Summary:

Congratulations! We have now created our own GUI based To-Do list using the Python Tkinter module.

Now you have with yourself a portable to-do list that you can open anywhere, even on your phone if you run this script through Pydroid 3. Based on further requirements, you can make many more improvements and make it even more fun!

Have fun coding!

Exit mobile version