To Do List in Python with Simple and Free Source Code 2023

To Do List In Python

Hello friends, in this article we will be building a simple to do list in python, you can use this application as a simple python project for your educational purposes and also for making your daily to do list. To run this to do list in python you will require a single tool that is a text editor to run our code which I have mentioned below.

To Do List in Python
To Do List in Python

First of all let’s discuss what is a – To Do List?

A to do list is a task manager where you can save your daily tasks that you need to complete on that specified day. It helps us to manage our daily tasks efficiently. Mostly this application is used to stay focused on our goals and responsibilities.

So Let’s Get Started –

Step 1 – Setting up the project

First let us create a folder in our local disk named “ToDoList”. Now we will work inside this directory, let’s open this folder in our text/code editor. I’m using VS code here.

To Do List Folder
To Do List Folder

Step 2 – Creating the To do List in Python

Create a new file in this folder named “app.py” and paste this code.

To Do List App and Code
To Do List App and Code

Source Code –

import tkinter as tk
from tkinter import ttk, messagebox

def add_task(event=None):
    global task_entry, tasks_text

    task = task_entry.get().strip()

    if task:
        with open('tasks.txt', 'r') as file:
            tasks = file.readlines()

        if task + '\n' in tasks:
            messagebox.showwarning("Duplicate Task", "Task already exists in the list.")
        else:
            with open('tasks.txt', 'a') as file:
                file.write(task + '\n')
            task_entry.delete(0, tk.END)
            messagebox.showinfo("Task Added", "Task added successfully!")
            view_tasks()  # Automatically view tasks after adding

    else:
        messagebox.showwarning("Error", "Please enter a task.")

def remove_selected_tasks():
    global tasks_text

    with open('tasks.txt', 'r') as file:
        tasks = file.readlines()

    with open('tasks.txt', 'w') as file:
        for idx, task in enumerate(tasks, start=1):
            task = task.strip()
            if task not in selected_tasks or not selected_tasks[task].get():
                file.write(task + '\n')

    view_tasks()
    messagebox.showinfo("Tasks Removed", "Selected tasks removed successfully!")

def view_tasks():
    global tasks_text, selected_tasks

    with open('tasks.txt', 'r') as file:
        tasks = file.readlines()

    tasks_text.config(state=tk.NORMAL)
    tasks_text.delete(1.0, tk.END)
    tasks_text.insert(tk.END, "View Tasks:\n\n")
    selected_tasks.clear()

    for idx, task in enumerate(tasks, start=1):
        task = task.strip()
        selected_tasks[task] = tk.BooleanVar()
        chk_task = ttk.Checkbutton(tasks_text, variable=selected_tasks[task])
        tasks_text.window_create(tk.END, window=chk_task)
        tasks_text.insert(tk.END, f"{idx}. {task}\n")

    tasks_text.config(state=tk.DISABLED)

def main():
    global task_entry, tasks_text, selected_tasks

    root = tk.Tk()
    root.title("To-Do List")
    root.geometry("400x400")
    root.configure(bg="#333333")

    label = tk.Label(root, text="Enter Task:", bg="#333333", fg="white", font=("Helvetica", 12))
    label.pack(pady=10)

    task_entry = tk.Entry(root, font=("Helvetica", 12), bg="white", fg="black")
    task_entry.pack(pady=5, padx=10, fill=tk.X)

    button_frame = ttk.Frame(root)
    button_frame.pack(pady=5, padx=10, fill=tk.X)

    add_button = ttk.Button(button_frame, text="Add Task", command=add_task)
    add_button.pack(side=tk.LEFT, padx=5, pady=5, fill='y', anchor='center')

    remove_button = ttk.Button(button_frame, text="Remove Selected", command=remove_selected_tasks)
    remove_button.pack(side=tk.LEFT, padx=5, pady=5, fill='y', anchor='center')

    view_button = ttk.Button(button_frame, text="View Tasks", command=view_tasks)
    view_button.pack(side=tk.LEFT, padx=5, pady=5, fill='y', anchor='center')

    tasks_text_frame = ttk.Frame(root)
    tasks_text_frame.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)

    tasks_text = tk.Text(tasks_text_frame, font=("Helvetica", 12), bg="#1e1e1e", fg="white", wrap=tk.WORD, state=tk.DISABLED)
    tasks_text.pack(fill=tk.BOTH, expand=True)

    scrollbar = ttk.Scrollbar(tasks_text_frame, command=tasks_text.yview)
    scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
    tasks_text.config(yscrollcommand=scrollbar.set)

    selected_tasks = {}

    # Bind the <Return> key event to add_task function
    root.bind("<Return>", add_task)

    root.mainloop()

if __name__ == "__main__":
    main()

Step 3 – To run this Application

Open new terminal and type “python app.py”. With this your application will execute with ease.

New terminal - python app.py
New terminal – python app.py

With these 3 simple steps you have built a – to do list in python.

Note : If you don’t have tkinter installed in your python then first install tkinter by using this command in your terminal “pip install tkinter”.

Features of this application –

  1. User Interface – We have used tkinter to build the application window. And it’s so user friendly even non coders can easily use this application with these simple steps.
  2. Adding Tasks – Any user can simply add any task at any time, just type your task inside the text area and press enter key or add button on the window.
  3. Removing Tasks – It is so simple to remove a task in this application just select the check box in front of the task and press remove button to remove that task.
  4. Displaying the List – For user convenience if the task are not loaded automatically we can press the View Task button to see all the previous tasks which were added.
  5. Save and Load Tasks – In this to do list application the tasks which are added by the user is saved automatically and whenever the user run the application the tasks are loaded again just click the View Task button to view the previously listed tasks. A text file is automatically created in the local disk where this application is saved to save and load the tasks.

FAQs

Do I need any programming experience to follow this tutorial?

No, this is simply made for beginners who atleast have some computer knowledge, by reading this article carefully they can execute this to do list in python.

Can I run this To Do List in Python application on any operating system?

Yes, this application is built using python, and python is a cross-platform language means it can be run on any operating systems like Windows, macOS or Linux without any modification.

What python version id required for this project?

Python 3 or above is recommended although it may run on lower versions but use the latest version if you face any error or something.

Links to visit –

To install Python and it’s libraries visit here – https://www.python.org/

To know more about innovation visit here – www.innovatequests.com

Want to create another AI project? visit here – https://innovatequests.com/create-ai-content-detector-for-free-2023/

Leave a Reply

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