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
- Import the Tkinter library
- Create to-do list application window
- Create application layout
- 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
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 you like our efforts? If Yes, please give DataFlair 5 Stars on Google
The enrollment button is not activated
Can you help me in this project? To create it?
I’m interested to learn Python
Suggest adding fg=’black’ to each Button widget. Button labels are white by default and blend in with the white button background.
Which version of Python is used? many functions are not defined in Python 3.
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?
Command in button section not working
import os
import json
import tkinter as tk
from tkinter import messagebox, simpledialog
class Task:
def __init__(self, description, completed=False):
self.description = description
self.completed = completed
def __repr__(self):
status = “✔️” if self.completed else “❌”
return f”{self.description} [{status}]”
class ToDoList:
def __init__(self, filename=”tasks.json”):
self.tasks = []
self.filename = filename
self.load_tasks()
def add_task(self, description):
self.tasks.append(Task(description))
self.save_tasks()
def view_tasks(self):
return self.tasks
def remove_task(self, index):
try:
self.tasks.pop(index)
self.save_tasks()
except IndexError:
messagebox.showerror(“Error”, “Invalid task number.”)
def mark_completed(self, index):
try:
self.tasks[index].completed = True
self.save_tasks()
except IndexError:
messagebox.showerror(“Error”, “Invalid task number.”)
def save_tasks(self):
with open(self.filename, “w”) as f:
json.dump([task.__dict__ for task in self.tasks], f, indent=4)
def load_tasks(self):
if os.path.exists(self.filename):
with open(self.filename, “r”) as f:
tasks = json.load(f)
self.tasks = [Task(**task) for task in tasks]
class ToDoApp(tk.Tk):
def __init__(self, todo_list):
super().__init__()
self.todo_list = todo_list
self.title(“To-Do List Application”)
self.attributes(‘-fullscreen’, True)
self.configure(bg=’lightblue’)
self.create_widgets()
self.refresh_tasks()
def create_widgets(self):
self.frame = tk.Frame(self, bg=’lightblue’)
self.frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
self.listbox = tk.Listbox(self.frame, font=(“Helvetica”, 18), bg=’white’, fg=’black’)
self.listbox.pack(fill=tk.BOTH, expand=True, pady=10)
self.add_button = tk.Button(self.frame, text=”Add Task”, command=self.add_task, bg=’#4CAF50′, fg=’white’, font=(“Helvetica”, 16))
self.add_button.pack(fill=tk.X, pady=5)
self.remove_button = tk.Button(self.frame, text=”Remove Task”, command=self.remove_task, bg=’#F44336′, fg=’white’, font=(“Helvetica”, 16))
self.remove_button.pack(fill=tk.X, pady=5)
self.mark_button = tk.Button(self.frame, text=”Mark Completed”, command=self.mark_task, bg=’#FFEB3B’, fg=’black’, font=(“Helvetica”, 16))
self.mark_button.pack(fill=tk.X, pady=5)
self.exit_button = tk.Button(self.frame, text=”Exit”, command=self.quit, bg=’#9E9E9E’, fg=’white’, font=(“Helvetica”, 16))
self.exit_button.pack(fill=tk.X, pady=5)
def refresh_tasks(self):
self.listbox.delete(0, tk.END)
for task in self.todo_list.view_tasks():
self.listbox.insert(tk.END, task)
def add_task(self):
description = simpledialog.askstring(“Task”, “Enter the task description:”)
if description:
self.todo_list.add_task(description)
self.refresh_tasks()
def remove_task(self):
try:
index = self.listbox.curselection()[0]
self.todo_list.remove_task(index)
self.refresh_tasks()
except IndexError:
messagebox.showerror(“Error”, “Select a task to remove.”)
def mark_task(self):
try:
index = self.listbox.curselection()[0]
self.todo_list.mark_completed(index)
self.refresh_tasks()
except IndexError:
messagebox.showerror(“Error”, “Select a task to mark as completed.”)
if __name__ == “__main__”:
todo_list = ToDoList()
app = ToDoApp(todo_list)
app.mainloop()
*Python Script that makes an To-Do app!*