Enhanced Task Manager Application with Tkinter in Python – GUI Edition (Part 2)

Posted by


In the first part of this tutorial, we discussed the basic layout of a To-Do list app using Tkinter in Python. In this part, we will talk about more advanced features such as adding new tasks, deleting tasks, marking tasks as completed, and saving and loading tasks from a file.

Adding New Tasks:
To add a new task to the list, we can create an entry field where the user can type in the new task. We will also need a button that will add the task to the list when clicked. Here’s how you can implement this feature:

# Create an entry field for new task input
new_task_entry = Entry(root, width=50)
new_task_entry.grid(row=1, column=0, padx=10, pady=10)

# Create a button to add the new task
add_task_button = Button(root, text="Add Task", command=add_task)
add_task_button.grid(row=1, column=1, padx=10, pady=10)

# Define the add_task function to add the new task to the list
def add_task():
    new_task = new_task_entry.get()
    if new_task:
        tasks.append(new_task)
        update_tasks_list()
        new_task_entry.delete(0, END)

In the add_task function, we retrieve the new task input from the entry field, add it to the tasks list, update the tasks list displayed on the screen, and clear the entry field for the next task.

Deleting Tasks:
To delete a task from the list, we can add a button next to each task that, when clicked, will remove the task from the list. Here’s how you can implement this feature:

# Create a list of buttons to delete tasks
delete_buttons = []
for i in range(len(tasks)):
    delete_buttons.append(Button(root, text="Delete", command=lambda x=i: delete_task(x)))
    delete_buttons[i].grid(row=i+2, column=2, padx=10, pady=5)

# Define the delete_task function to delete the selected task
def delete_task(index):
    del tasks[index]
    update_tasks_list()

In the above code, we create a list of delete_buttons where each button is associated with a task in the tasks list. When a button is clicked, the delete_task function is called with the index of the task to be deleted.

Marking Tasks as Completed:
To mark a task as completed, we can add a checkbox next to each task that, when checked, will change the appearance of the task to indicate that it has been completed. Here’s how you can implement this feature:

# Create a list of checkboxes to mark tasks as completed
complete_checkboxes = []
for i in range(len(tasks)):
    complete_checkboxes.append(Checkbutton(root, command=lambda x=i: mark_completed(x)))
    complete_checkboxes[i].grid(row=i+2, column=0, padx=10, pady=5)

# Define the mark_completed function to update the appearance of a task
def mark_completed(index):
    if complete_checkboxes[index].get() == 1:
        tasks[index] = "✓ " + tasks[index]
    else:
        tasks[index] = tasks[index][2:]
    update_tasks_list()

In the above code, we create a list of complete_checkboxes where each checkbox is associated with a task in the tasks list. When a checkbox is checked, the mark_completed function is called with the index of the task to be marked as completed. The appearance of the task is changed by adding a checkmark symbol before the task text.

Saving and Loading Tasks:
To save the tasks to a file and load them back when the app is restarted, we can use the pickle module in Python. Here’s how you can implement this feature:

import pickle

# Define functions to save and load tasks
def save_tasks():
    with open('tasks.pkl', 'wb') as file:
        pickle.dump(tasks, file)

def load_tasks():
    global tasks
    try:
        with open('tasks.pkl', 'rb') as file:
            tasks = pickle.load(file)
    except:
        tasks = []
    update_tasks_list()

In the above code, we define functions save_tasks and load_tasks to save the tasks list to a file and load it back respectively. The pickle module is used to serialize and deserialize the tasks list to and from the file.

To call the load_tasks function when the app starts, you can add the following line to the main script:

load_tasks()

With these advanced features, your To-Do list app using Tkinter in Python is now more powerful and user-friendly. Feel free to customize the app further by adding more features and improving the user interface. Happy coding!