In this blog, we’re diving into a fun and useful Python project: building a To-Do List App using Tkinter, complete with a Pie Chart that visually represents your task progress! If you’re learning Python and want a hands-on project that combines Tkinter for the GUI and Matplotlib for a pie chart, this one’s for you.
Let’s break down this app into two core components:
- To-Do List functionality using Tkinter.
- Pie Chart visualization to show task completion, using Matplotlib.\
What You Will Learn:
- Basics of Tkinter for building graphical interfaces.
- Using Listbox for task management.
- Incorporating Matplotlib to create a pie chart that updates based on task completion.
Let’s get started!
Step 1: Setting Up the Environment
Before we dive into the code, make sure you have the following libraries installed:
pip install tkinter matplotlib
Related Post: Check out our Beginner’s Guide to Python for more Python learning resources.
Step 2: Building the To-Do List App with a Pie Chart Using Tkinter
Tkinter makes it super easy to create a simple to-do list. We’ll use a Listbox
to display tasks, an entry field to add new tasks, and a button to mark them as complete.
Here’s the code to set up the basic to-do list:
import tkinter as tk from tkinter import messagebox import matplotlib.pyplot as plt # Initialize Tkinter root = tk.Tk() root.title("To-Do List with Pie Chart") tasks = [] completed_tasks = 0 # Function to add task def add_task(): task = task_entry.get() if task: tasks.append({"task": task, "status": "Incomplete"}) update_task_list() task_entry.delete(0, tk.END) else: messagebox.showwarning("Input Error", "Please enter a task") # Function to mark task as complete def complete_task(): global completed_tasks try: task_index = task_listbox.curselection()[0] tasks[task_index]["status"] = "Complete" completed_tasks += 1 update_task_list() update_pie_chart() except IndexError: messagebox.showwarning("Selection Error", "Please select a task") # Function to update the Listbox def update_task_list(): task_listbox.delete(0, tk.END) for task in tasks: task_listbox.insert(tk.END, f"{task['task']} - {task['status']}") # Function to update the Pie Chart def update_pie_chart(): total_tasks = len(tasks) incomplete_tasks = total_tasks - completed_tasks if total_tasks == 0: return plt.clf() plt.pie([completed_tasks, incomplete_tasks], labels=["Complete", "Incomplete"], autopct='%1.1f%%', startangle=90, colors=["#4CAF50", "#FFC107"]) plt.title("Task Completion") plt.show() # Creating the GUI elements task_entry = tk.Entry(root, width=40) task_entry.pack(pady=10) add_button = tk.Button(root, text="Add Task", command=add_task) add_button.pack(pady=5) complete_button = tk.Button(root, text="Complete Task", command=complete_task) complete_button.pack(pady=5) task_listbox = tk.Listbox(root, height=10, width=50) task_listbox.pack(pady=20) root.mainloop()
Pro Tip: Explore more projects like this in our Python Projects section!
Step 3: Breaking Down the Code
- Tkinter Window: We start by initializing a Tkinter window with
tk.Tk()
. - Task Entry and Buttons: Users can input tasks in the entry field, and use buttons to add or mark tasks as complete.
- Listbox: The
Listbox
widget is used to display tasks and their completion status. - Task Management: Tasks are stored in a list of dictionaries, where each task has a name and status (“Incomplete” or “Complete”).
- Updating Tasks: The
update_task_list
function refreshes the tasks in the Listbox.
Step 4: Adding a Pie Chart for Task Completion
Next, let’s add a pie chart using Matplotlib. The pie chart will visually display the ratio of completed tasks to incomplete ones. Every time a task is marked as complete, the pie chart will update.
- Matplotlib Integration: The
update_pie_chart
function calculates the number of completed and incomplete tasks, then plots a pie chart. - Colors: We use two different colors to represent the completed (green) and incomplete (yellow) tasks.
When a task is marked as complete, the update_pie_chart()
function is triggered, which generates a new pie chart reflecting the updated state.
Step 5: Testing the App
Once you’ve added all the components, run the script. You should now have a window where you can add tasks, mark them as complete, and view your progress with a pie chart!
Full Code Example:
import tkinter as tk from tkinter import messagebox import matplotlib.pyplot as plt # Initialize Tkinter root = tk.Tk() root.title("To-Do List with Pie Chart") tasks = [] completed_tasks = 0 # Function to add task def add_task(): task = task_entry.get() if task: tasks.append({"task": task, "status": "Incomplete"}) update_task_list() task_entry.delete(0, tk.END) else: messagebox.showwarning("Input Error", "Please enter a task") # Function to mark task as complete def complete_task(): global completed_tasks try: task_index = task_listbox.curselection()[0] tasks[task_index]["status"] = "Complete" completed_tasks += 1 update_task_list() update_pie_chart() except IndexError: messagebox.showwarning("Selection Error", "Please select a task") # Function to update the Listbox def update_task_list(): task_listbox.delete(0, tk.END) for task in tasks: task_listbox.insert(tk.END, f"{task['task']} - {task['status']}") # Function to update the Pie Chart def update_pie_chart(): total_tasks = len(tasks) incomplete_tasks = total_tasks - completed_tasks if total_tasks == 0: return plt.clf() plt.pie([completed_tasks, incomplete_tasks], labels=["Complete", "Incomplete"], autopct='%1.1f%%', startangle=90, colors=["#4CAF50", "#FFC107"]) plt.title("Task Completion") plt.show() # Creating the GUI elements task_entry = tk.Entry(root, width=40) task_entry.pack(pady=10) add_button = tk.Button(root, text="Add Task", command=add_task) add_button.pack(pady=5) complete_button = tk.Button(root, text="Complete Task", command=complete_task) complete_button.pack(pady=5) task_listbox = tk.Listbox(root, height=10, width=50) task_listbox.pack(pady=20) root.mainloop()
Conclusion