To Do List in Python with Source Code

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

The best way to master a programming language and start our career as a developer is to develop projects to solve real-life problems.

In this article, we are going to solve one such real-life problem, the problem of keeping a list of tasks one might want to do in a day.

Maintaining a To-do list always helps to organize the day and work better and make it the most productive.

Let’s start with the development of python To-do list.

To Do List in Python

We are going to implement a simple python to-do list in which we can add a task and delete it when it’s done. We will be using a Python package called Tkinter which is a widely used Python GUI library. It is shipped with python, so we do not have to download or install it separately, we can just import and start with it.

A GUI is basically a medium to interact and present information to the users. One of the major advantages of using Tkinter is that it works well on any machine be it windows, linux, or macOS.

Project Prerequisites

To work on to do list in python, basic understanding of python programming language and Tkinter, especially Tkinter widgets would be helpful. But don’t worry as this article will provide an explanation of every line of code as we go about building this python project. You are free to choose any IDE of your choice (Pycharm, VSCode, etc.).

Download To-do List Python Project

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

Steps to develop To-do List Project

  1. Import the Tkinter library
  2. Create to-do list application window
  3. Create application layout
  4. Define to do list functions

1. Import the Tkinter library

#importing packages 
from  tkinter import * 
import tkinter.messagebox

Explanation

  • In the first line, we are importing Tkinter which is basically a standard GUI library in Python and every possible object of it.
  • In the second line, we are importing an important widget from the Tkinter library called the message box which is used to display messages in a python to-do list project.

2. Create To-do list application window

window=Tk()
#giving a title
window.title("DataFlair Python To-Do List APP")
window.mainloop()

Explanation

  • Tk() is a top-level widget which is used to create the main application window in which we will be building python to-do list application.
  • The title() method is used to give a name to our application which is basically displayed at the top.
  • The mainloop() method basically runs the Tkinter event loop, it runs and displays everything we have written in our code.

3. Create application layout

#creating the initial window
window=Tk()
#giving a title
window.title("DataFlair Python To-Do List APP")


#Frame widget to hold the listbox and the scrollbar
frame_task=Frame(window)
frame_task.pack()

#to hold items in a listbox
listbox_task=Listbox(frame_task,bg="black",fg="white",height=15,width=50,font = "Helvetica")  
listbox_task.pack(side=tkinter.LEFT)

#Scrolldown in case the total list exceeds the size of the given window 
scrollbar_task=Scrollbar(frame_task)
scrollbar_task.pack(side=tkinter.RIGHT,fill=tkinter.Y)
listbox_task.config(yscrollcommand=scrollbar_task.set)
scrollbar_task.config(command=listbox_task.yview)


#Button widget 
entry_button=Button(window,text="Add task",width=50,command=entertask)
entry_button.pack(pady=3)

delete_button=Button(window,text="Delete selected task",width=50,command=deletetask)
delete_button.pack(pady=3)

mark_button=Button(window,text="Mark as completed ",width=50,command=markcompleted)
mark_button.pack(pady=3)


window.mainloop()

Explanation

  • Frame() method is basically a container widget within our main window and it is used to hold different widgets. It takes an argument that is our main window.
  • pack() method is a geometry manager which organizes the widget properly before placing it in the main window in a level order fashion until explicitly mentioned.
  • Listbox() widget is going to store all the tasks that we list in python to-do list application. We have given three arguments out of which the first one is where we want our widgets to be placed. If the height and width is not given, it takes the default value. Also it would take the default value as window, if the frame_task is not explicitly mentioned.
  • Scrollbar() widget is used to place a scroll bar in case the total number of lists exceeds the given dimension of the to-do list window. Like every other widget it takes an argument as to where it should be placed. We pack it to the RIGHT of the frame_task widget then we set it on the listbox_task widget and after that we configure it on the vertical axis given by the command listbox_task.yview.
  • Button() widget is used to create a button. We want the buttons in our main to-do list window so the input window is given. Then the text which will be displayed on the button is specified and at last in the command input a function is given which will be called when the button is clicked.

4. Define To-do list functions

def entertask():
    #A new window to pop up to take input
    input_text=""
    def add():
        input_text=entry_task.get(1.0, "end-1c")
        if input_text=="":
            tkinter.messagebox.showwarning(title="Warning!",message="Please Enter some Text")
        else:
            listbox_task.insert(END,input_text)
            #close the root1 window
            root1.destroy()
    root1=Tk()
    root1.title("Add task")
    entry_task=Text(root1,width=40,height=4)
    entry_task.pack()
    button_temp=Button(root1,text="Add task",command=add)
    button_temp.pack()
    root1.mainloop()
    

#function to facilitate the delete task from the Listbox
def deletetask():
    #selects the selected item and then deletes it 
    selected=listbox_task.curselection()
    listbox_task.delete(selected[0])
#Executes this to mark completed 

def markcompleted():
    marked=listbox_task.curselection()
    temp=marked[0]
    #store the text of selected item in a string
    temp_marked=listbox_task.get(marked)
    #update it 
    temp_marked=temp_marked+" ✔"
    #delete it then insert it 
    listbox_task.delete(temp)
    listbox_task.insert(temp,temp_marked)

Explanation

  • A good coding practice is to place all the functions in the beginning of the code. These functions will be called when the buttons are clicked and will perform the work accordingly.
  • entertask() is used to add a task to the listbox. When this function is called another window opens in which we can write our task and it is added to the listbox. add() function is called when the Add task button is clicked in the opened window.
    Entered input is stored in input_text, if the variable is empty then a warning message is displayed else it is inserted in the listbox of python to-do list.
  • deletetask() is used to delete a selected item, listbox_task.curselection() function returns a tuple in which the first element is the index of the selected item. Then, delete() function is used to delete the item corresponding to the index in python to do list application
  • markcompleted() is used to mark an item in to-do list as completed. In this function, we store the contents of the selected task in a variable called temp_marked.
    We update the value by adding a ” ✔” at the end. After this, the delete() function deletes the selected task, and insert() function inserts the updated value at that particular index.

Python To Do List Output

python to do list

Summary

Congratulations! We have successfully taken our first step to become a python developer and completed python to do list project

We learned about Tkinter, an important GUI library in Python, and various widgets that it offers to manipulate and get the desired tasks done through this python to-do list project. We also learned about the basics of python programming and in this way we have been successful in building our very own To-do list app.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

7 Responses

  1. Mary Oguntuase says:

    The enrollment button is not activated

  2. Nehemiah Walyo says:

    I’m interested to learn Python

  3. Matt says:

    Suggest adding fg=’black’ to each Button widget. Button labels are white by default and blend in with the white button background.

  4. Ashutosh Singh says:

    Which version of Python is used? many functions are not defined in Python 3.

  5. Ashutosh Singh says:

    Very very poor and bad design. It not even close to a Todo app. It doesn’t save the tasks. Once the app is closed every task added is lost?

  6. xyz says:

    Command in button section not working

Leave a Reply

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