Create To Do List In Python

To Do List is now part of our daily life. It helps us in time management, prioritization, reminders, etc.

You might have created a to-do list on pen-paper so that you don’t miss deadlines or any important tasks. In this project, we will develop a To Do list using python and Tkinter.

To Do List Functionalities:

  • Add a task
  • Delete a task
  • Mark a task Complete
  • Display all tasks

Project Prerequisites

To install the required libraries, you can use pip installer from the terminal:

Install mysql connector:

pip install mysql-connector-python

Install tkinter:

pip install tkinter

Download To Do List Python Code

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

Database Creation Steps:

 create database db;
 
 use db;
 
create table ToDoList(
    id varchar(10),
    task varchar(100),
    status varchar(5));
 
 
desc ToDoList;

To Do List Database:

mysql table desc

We need following three attributes to describe a task

  • Id: acts as a primary key
  • Task: description of task
  • Status: denotes whether task is completed or not

Create To Do List Python Files

1. main.py

from tkinter import *
import mysql.connector
from addTask import*
 
window=Tk()
window.title("ProjectGurukul To-Do List")
db = mysql.connector.connect(host ="localhost",user = "root",password = 'password',database='db')
cursor = db.cursor()
 
greet = Label(window, font = ('arial', 20,'bold'), text = "To Do List")
greet.grid(row = 0,columnspan = 3)
 
L = Label(window, font = ('arial', 15), text = "Add a task")
L.grid(row = 2,column =1)
 
btn=Button(window,text="Add",command=add,padx=10,pady=5,bg="DodgerBlue2",fg="white",font = ('arial', 15))
btn.grid(row=2,column=3)
 
def delete(*args, **kwargs):
    tid=args[0]
 
    sqlquery= "Delete from ToDoList  where id='" + tid +"';"
    print(sqlquery)
 
    try:
        cursor.execute(sqlquery)
        db.commit()
        messagebox.showinfo('Success',"Task Deleted Successfully")
    except:
        messagebox.showinfo("Error","Cannot access Database")
 
def update(*args, **kwargs):
    tid=args[0]
    print("called")
    sqlquery= "Update ToDoList set status='YES' where id='" + tid +"';"
    print(sqlquery)
 
    try:
        cursor.execute(sqlquery)
        db.commit()
        messagebox.showinfo('Success',"Task Updated Successfully")
    except:
        messagebox.showinfo("Error","Cannot access Database")
 
sqlquery= "select * from toDoList;"
 
try:
    cursor.execute(sqlquery)
    print(sqlquery)
    L = Label(window, font = ('arial', 15), text = "%-10s%-50s%-10s"%('Tid','Task','Completed'))
    L.grid(row = 3,columnspan = 4)
 
    x=4
    for i in cursor:
        L = Label(window, font = ('arial', 15), text = "%-10s%-50s%-10s"%(i[0],i[1],i[2]))
        L.grid(row = x,column = 1)
 
        btn=Button(window,text="Delete",command=lambda arg=i[0], kw="delete" : delete(arg, o1=kw),padx=10,pady=5,bg="DodgerBlue2",fg="white",font = ('arial', 15))
        btn.grid(row=x,column=4)
 
        btn=Button(window,text="Done",command=lambda arg=i[0], kw="update" : update(arg, o1=kw),padx=10,pady=5,bg="DodgerBlue2",fg="white",font = ('arial', 15))
        btn.grid(row=x,column=5)
 
        x+=1   
 
except:
    messagebox.showinfo("Error","Cannot access Database")
 
mainloop()

In this file, we create a tkinter button for adding a task in the list, this add function is explained later in the same article. After that, we are simply fetching the records from the database using MySQL connector and displaying them as a list.

There are two buttons in front of each task representing the functionalities of our app (delete and update i.e. mark as done).

to do list output

 

2. Deleting Task

For deletion, we are passing the task id to the delete function and then we are simply deleting the record from the database using MySQL connector.

delete task

3. Updating a Task

We are passing the id of completed task to the update function and then we are accessing and updating the status of task using MySQL connector.

4. addTask.py

from tkinter import *
from tkinter import messagebox
import mysql.connector
 
def add_db():
    
    global id
    global task
 
    tid=id.get()
    ttask=task.get()
    
    db = mysql.connector.connect(host ="localhost",user = "root",password = 'password',database='db')
    cursor = db.cursor()
 
    sqlquery= "insert into ToDoList values('" + tid +"','"+ttask+"','NO');"
    print(sqlquery)
 
    try:
        cursor.execute(sqlquery)
        db.commit()
        messagebox.showinfo('Success',"Task added Successfully")
    except:
        messagebox.showinfo("Error","Cannot access Database")
    
    window.destroy()
 
def add():
 
    global id
    global task
    
    window=Tk()
    window.title("ProjectGurukul To-Do List")
 
    greet = Label(window, font = ('arial', 20), text = "Add a Task !")
    greet.grid(row = 0,columnspan = 3)
 
    #----------id-------------------
    L = Label(window, font = ('arial', 15, 'bold'), text = "Enter Task id: ")
    L.grid(row = 2, column = 1)
 
    L = Label(window, font = ('arial', 15), text = "   ")
    L.grid(row = 2, column = 2)
 
    id=Entry(window,width=5,font =('arial', 15))
    id.grid(row=2,column=3)
 
    #------------------task---------------------
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "Enter task: ")
    L.grid(row = 3, column = 1)
 
    L = Label(window, font = ('arial', 15), text = "   ")
    L.grid(row = 3, column = 2)
 
    task=Entry(window,width=5,font =('arial', 15))
    task.grid(row=3,column=3)
 
    submitbtn=Button(window,text="Submit",command=add_db,bg="DodgerBlue2",fg="white",font = ('arial', 15, 'bold'))
    submitbtn.grid(row=5,columnspan=3)
        
    pass

We are asking id and the task description from user in the add function and then we are calling the add_db function to insert the new task in the database. We are using the Mysql connector to access, update and delete records.

After successful insertion of the task, a message will be flashed on the screen, like the one shown in the below image.

add task

Summary

We have finally completed the project in five simple python files. You can add more features in this project to make it more effective and easy to use.
Happy Coding !!!

Leave a Reply

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