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!

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

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:

  • tasks.txt – The text file where all our tasks will be stored
  • main.py – The python script file

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:

  • To initialize the to-do list window, you need to initialize the Tk() class to a variable. Then, you need to set the following attributes:
    • title() method is used to give a title to the project window.
    • geometry() method is used to set the initial geometry of window.
    • resizable() method is used to allow/deny the user the permission to resize the python to-do list window. It takes truthy and falsy values and arguments to the parameters width and height.
    • config() method is used to configure some extra attributes of the window, like bg or background to set the background color.
    • update() and .mainloop() methods are used to put the window in a loop to prevent it from closing nanoseconds after it opens.
    • Note: These lines of code will be considered the last lines that will be read by the interpreter to run the script.
  • The Label class is used to create a label that displays the static screen on the window and set the following parameters to it:
    • master attribute, positional argument root in this case, is used to specify the parent widget of the widget.
    • text attribute is used to mention the text that will be displayed on the label.
    • font is used to specify the font family, size and effects on the text.
    • wraplength attribute is the length after which the text will be wrapped on the window.
  • The Button class is used to create a button on the screen that executes a function as a command when pressed. You need to give it the parameters as mentioned in the code to add it to the screen.
    • command attribute is the function that will run when the button is pressed. You do not need to use the lambda keyword if the function requires no arguments.
    • width attribute is the width of the button in pixels.
  • The Entry class is used to add an input widget to the window that accepts input data from the user. The attributes and methods of this class used in this python to do list project are:
    • The .get() method is used to get the text inputted by the user in the widget. It requires no arguments for this widget.
  • The Listbox class is used to add a list box to the window that displays multiple items on the screen. Its attributes, parameters and methods are:
    • selectbackground attribute is used to mention the color of the background once an item is selected.
    • width attribute represents the number of characters visible.
    • height attribute represents the number of items visible on the screen.
    • yscrollcommand attribute is used to pack the widget to a scrollbar that will navigate up and down the widget.
    • insert() method is used to insert an element to the widget. It takes 2 arguments, index and element.
      • index can either be a number or an acceptable tkinter constant, END, in this case.
      • The element is the item to be added to the widget.
    • get() method is used to get an element from the widget. It takes only one – index argument.
      • The index is either a number or an acceptable tkinter constant, which in this case is ACTIVE to get the selected element.
    • yview() method is used to make the widget vertically scrollable.
  • The Scrollbar class is used to add a scroll bar to the widget on the window to navigate up and down, or right to left in said widget. Its attributes (undiscussed) are:
    • The orient attribute is used to mention whether the scrollbar will scroll vertically or horizontally.
  • The with open(“<filename>.<extension>”, “<mode>”) as alias is used to open a file in the <mode> mode, to perform actions on it.
    • The modes are:
      • “r”, for reading a file
      • “w”, for writing a file
      • “a”, for appending text to the file
      • “x”, to create a file
      • “r+”, for reading and writing a file
      • “t”, to open the file in text mode (default)
      • “b”, to open the file in binary mode.
    • The actions that can be performed on it are:
      • To close – .close() method
      • To rewrite the file completely – .write(‘’) method when opened in “w” or “r+” mode
      • To add text to the file – .write(text_to_add) method when opened in “a” mode
      • To reduce the size – .truncate(int(<size in bytes>)) method
      • To store all the lines in a list – .readlines() method
      • To remove a line – .remove(line_to_remove) method

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:

  • In this step, we will create two functions, one to add items to the python to-do list and the other to delete an item from the list.
  • In the add_item function, the user will give it to arguments, one an Entry object and the other a Listbox object.
    • In this function, we will give the Entry object’s user-provided text to a variable and then insert that variable to the last of our listbox object, and the last line of our text file.
  • In the delete_item function, the user will give the function one argument that will be a listbox object.
    • In this function, we will delete the item selected in the listbox object. We will first remove that element from the listbox and then go through every text file line to see if anything matches. If it does, we remove it too.

Python To-do List Output

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!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.