Site icon DataFlair

To Do List in Python with Source Code

python project to do list

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

2. Create To-do list application window

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

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Explanation

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

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

Python To Do List Output

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.

Exit mobile version