Personal Expense Tracker Using Python (TUTORIAL)




Personal Expense Tracker using Python

Hi Programmers, 👋

It’s been a year since my last post, but I’m excited to announce that I’m back! From now on, I will aim to post a new project every week. Thank you all for your patience and support. Over the past year, I’ve been busy with other commitments and couldn’t focus on the blog. However, I assure you that the practical Python project tutorials will continue from now on. I have reworked some of my tutorials and working on other tutorials too.

Thank you for sticking with me!

In this tutorial, we will create a Personal Expense Tracker 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

import tkinter as tk
from tkinter import messagebox
from tkcalendar import DateEntry
import csv

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 (we will delve into this further later). Additionally, the DateEntry widget from tkcalendar will enable us to create a field for users to input dates.

Step-2: Creating the Root app object

root = tk.Tk()
root.title("Personal Expense Tracker")

We are creating a root app object which will act us the GUI.

Step-3: Adding Labels and Entries

# Labels and Entries for expense details
tk.Label(root, text="Date (YYYY-MM-DD)").grid(row=0, column=0)
tk.Label(root, text="Amount").grid(row=1, column=0)
tk.Label(root, text="Category").grid(row=2, column=0)
tk.Label(root, text="Description").grid(row=3, column=0)

date_entry = DateEntry(root)
amount_entry = tk.Entry(root)
category_entry = tk.Entry(root)
description_entry = tk.Entry(root)

date_entry.grid(row=0, column=1)
amount_entry.grid(row=1, column=1)
category_entry.grid(row=2, column=1)
description_entry.grid(row=3, column=1)

tk.Button(root, text="Add Expense", command=add_expense).grid(row=4, column=0, columnspan=2)

At this stage, we construct a Label and Entry for each detail of the expense, including date, amount, category, and description. The ".grid" method is used to organize each Label and Entry in a row-wise fashion. Lastly, we introduce a button linked to the "add_expense" command, which acts as the central feature of this project.

Step-4: Defining the Function

This is the key part of our project.

def add_expense():
    date = date_entry.get()
    amount = amount_entry.get()
    category = category_entry.get()
    description = description_entry.get()

    if date and amount and category and description:
        with open('expenses.csv', 'a', newline='') as file:
            writer = csv.writer(file)
            writer.writerow([date, amount, category, description])
        messagebox.showinfo("Success", "Expense added successfully")
    else:
        messagebox.showerror("Error", "All fields are required")

  • We will define a function named "add_expense". We will retrieve the user inputs with ".get()" method from our entries.
  • We will then verify that all fields are complete; if not, we will display an error using the messagebox method.
  • If all information is given then, we are opening a "expenses.csv" file using "open()" method as "file" object. The parameters of open function are file_path, accessibility of the function (read, write) (Here 'a' represent write).
  • Next, we will create a "writer" object using "csv.writer()" method, here we will give file object as parameter.
  • Finally, we will write the user inputs to the CSV file and notify that the process is complete.

Step-5: Execute the app object

root.mainloop()

Finally, we will execute the app object in a loop to generate the output.

We have developed a Personal Expense Tracker 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.🙂