Personal To-Do List Application using Python
Hi Programmers, 👋
In this tutorial, we will create a Personal To-Do List Application using Python. This project is both simple and enjoyable to build. I will explain the creation process of the project step by step, so make sure to pay close attention to fully understand the tutorial. Let's begin.
Output:
Step-1: Importing Modules:
We are utilizing the tkinter
module for the GUI aspect of our project. The messagebox
function will alert us to the occurrence of an event by displaying a pop-up message. The simpledialog
module will allow us to prompt the user for input in a dialog box. Additionally, the json
module will help us save and load tasks.
Step-2: Creating the Root App Object:
We are creating a root app object which will act as the GUI.
Step-3: Defining the Functions
Add Task Function:
We will define a function named add_task
. We will retrieve the user input with the .get()
method from our entry. If the input is valid, we will add the task to the list and save it. If not, an error message will be displayed.
View Tasks Function:
We will define a function named view_tasks
to display all tasks with their statuses.
Logic:
- Initialize
task_list
String: Start with an empty string to accumulate task details. - Loop Through Tasks: Iterate over the
tasks
list with an index (enumerate
provides both the index and the task). - Determine Status: Check if the task is completed and set the
status
string accordingly. - Accumulate Task Details: Append each task's details (index, task name, and status) to the
task_list
string. - Display Tasks: Show the accumulated
task_list
string in a messagebox.
Mark Task as Completed Function:
We will define a function named mark_task_completed
to mark a selected task as completed.
Logic:
- Ask for Task Number: Prompt the user to enter the task number to mark as completed.
- Validate Input: Check if the entered task number is within the valid range of task indices.
- Mark Task: Set the
completed
status of the specified task toTrue
. - Save Tasks: Call
save_tasks()
to save the updated task list to the JSON file. - Show Success Message: Inform the user that the task has been successfully marked as completed.
- Error Handling: Display an error message if the entered task number is invalid.
Delete Task Function:
We will define a function named delete_task
to delete a selected task.
Logic:
- Ask for Task Number: Prompt the user to enter the task number to delete.
- Validate Input: Check if the entered task number is within the valid range of task indices.
- Delete Task: Remove the specified task from the
tasks
list usingpop()
. - Save Tasks: Call
save_tasks()
to save the updated task list to the JSON file. - Show Success Message: Inform the user that the task has been successfully deleted.
- Error Handling: Display an error message if the entered task number is invalid.
Step-5: Saving and Loading Tasks:
Save Tasks Function:
We will define a function named save_tasks
to save the tasks to a JSON file.
Load Tasks Function:
We will define a function named load_tasks
to load the tasks from the JSON file. If the file does not exist, it returns an empty list.
Step-5: Adding Labels, Entries, and Buttons:
At this stage, we construct a Label
and Entry
for the task. The .grid
method is used to organize each Label
and Entry
in a row-wise fashion. Additionally, we introduce buttons linked to the respective commands for adding, viewing, marking, and deleting tasks.
Step-6: Initialize the Task List:
Step-7: Execute the App Object:
Finally, we will execute the app object in a loop to generate the output.
We have developed a Personal To-Do List Application in Python. You can now enhance this code by adjusting the parameters of the labels and entry fields to improve the application's visual appeal. Fully engage with and comprehend the code, so you can devise innovative solutions for a range of problems using Python.
Happy Learning.🙂