Python Project – Expense Tracker
The Expense Tracker GUI application is a user-friendly interface for managing personal expenses. Built using Python’s Tkinter library, the application offers functionalities to add, view, and delete expenses stored in a JSON file (expenses.json).
Objectives of Python Expense Tracker
- Allow users to interact with the application through a straightforward command-line interface.
- Save expense records in a file to ensure data persistence across sessions.
- Implement basic CRUD (Create, Read, Update, Delete) operations for managing expenses.
Project Setup
Required Libraries
The project requires the following standard Python libraries:
- json: For reading and writing data to the JSON file.
- Tkinter: Provides GUI elements and interactions for the application.
- datetime: For recording the date and time of each expense entry.
Prerequisites for Python Expense Tracker
- IDE – Visual Studio Code
- Python Environment
- JSON
- The user should have a good understanding of the above topics
Download the Python Expense Tracker Project
Please download the source code of the Python Expense Tracker Project: Python Expense Tracker Project Code.
Step-by-Step Implementation of Python Expense Tracker
1. Loading Expenses
- Using a try-catch block, open the DATA_FILE and read it in read mode.
- except FileNotFoundError catches the FileNotFoundError exception, i.e., raised when the file specified is not found
def load_expenses():
try:
with open(DATA_FILE, 'r') as file:
return json.load(file)
except FileNotFoundError:
return []
Saving Expenses
- with open(DATA_FILE, ‘w’) as file: This opens the file specified by DATA_FILE in write mode.
- json.dump(expenses, file, indent=4): Using json. We can dump the expenses data to the file in JSON format.
def save_expenses(expenses):
with open(DATA_FILE, 'w') as file:
json.dump(expenses, file, indent=4)
2. Adding an Expense
- expenses = load_expenses(): It calls the load expense function to fetch the current list of costs from the JSON file.
- An expense dictionary is defined to represent new expenses.
- The new expense is appended and saved using the expense function.append() & save_expense()
- update_listbox(): This function refreshes the user interface to display the updated list of expenses.
def add_expense(amount, category, description):
expenses = load_expenses()
expense = {
'amount': amount,
'category': category,
'description': description,
'date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
}
expenses.append(expense)
save_expenses(expenses)
update_listbox()
3. Viewing Expenses
- expenses = load_expenses(): It calls the load expense function to fetch the current list of costs from the JSON file.
- It displays all recorded expenses, showing the date, category, amount, and description for each entry..
def view_expenses(): expenses = load_expenses() return expenses
4. Deleting an Expense
- expenses = load_expenses(): It calls the load expense function to fetch the current list of costs from the JSON file.
- Using an if statement, we check whether the index is within the list’s valid range.
- The expense list is updated and saved using save_expense().
- update_listbox(): This function refreshes the user interface to display the updated list of expenses.
def delete_expense(index):
expenses = load_expenses()
if 0 <= index < len(expenses):
del expenses[index]
save_expenses(expenses)
update_listbox()
5. Adding & Deleting using GUI
- add_expense_gui() function is responsible for adding a new expense based on user input from the GUI Entry widgets (amount_entry, category_entry, description_entry)
- The delete_expense_gui() function handles deleting costs based on the user’s selection from the Listbox (expenses_listbox).
def add_expense_gui():
amount = float(amount_entry.get())
category = category_entry.get()
description = description_entry.get()
add_expense(amount, category, description)
clear_entries()
def delete_expense_gui():
try:
index = expenses_listbox.curselection()[0]
delete_expense(index)
except IndexError:
pass
6. Update & Clear Entries
- expenses = view_expenses(): It calls the view expense function to fetch the current list of expenses.
- expenses_listbox.delete(0, tk.END): It clears all current entries in the expenses_listbox widget by deleting from the first item, i.e., index 0.
- clear_entries() function clears the input fields (Entry widgets) in the GUI after an expense has been added.
def update_listbox():
expenses = view_expenses()
expenses_listbox.delete(0, tk.END)
for expense in expenses:
expenses_listbox.insert(tk.END, f"{expense['date']}: {expense['category']} - ₹{expense['amount']} ({expense['description']})")
def clear_entries():
amount_entry.delete(0, tk.END)
category_entry.delete(0, tk.END)
description_entry.delete(0, tk.END)
7. Tkinter Setup
- It initialises the main Tkinter-based GUI application for an Expense Tracker.
- It establishes input fields for amount, category, and description using Entry widgets, along with buttons for adding and deleting expenses.
- A Listbox is used to display existing expenses, which are dynamically updated by the update_listbox() function.
root = tk.Tk()
root.title("Expense Tracker")
tk.Label(root, text="Amount:").grid(row=0, column=0, padx=10, pady=5)
amount_entry = tk.Entry(root)
amount_entry.grid(row=0, column=1, padx=10, pady=5)
tk.Label(root, text="Category:").grid(row=1, column=0, padx=10, pady=5)
category_entry = tk.Entry(root)
category_entry.grid(row=1, column=1, padx=10, pady=5)
tk.Label(root, text="Description:").grid(row=2, column=0, padx=10, pady=5)
description_entry = tk.Entry(root)
description_entry.grid(row=2, column=1, padx=10, pady=5)
add_button = tk.Button(root, text="Add Expense", command=add_expense_gui)
add_button.grid(row=3, column=0, columnspan=2, padx=10, pady=5)
delete_button = tk.Button(root, text="Delete Expense", command=delete_expense_gui)
delete_button.grid(row=4, column=0, columnspan=2, padx=10, pady=5)
expenses_listbox = tk.Listbox(root, width=50, height=10)
expenses_listbox.grid(row=5, column=0, columnspan=2, padx=10, pady=5)
update_listbox()
root.mainloop()
Python Expense Tracker Output
1. Application Interface
2. Add Expense
3. Added Expense

4. Delete Expense
5. Deleted Expense
Applications
- Filtering and Sorting: Implement features to filter expenses by date, category, or amount and sort them accordingly.
- Data Validation: Add input validation to ensure data integrity (e.g., by ensuring that the entered amount is a positive number).
- Reporting: Generate monthly or yearly expense reports and summaries.
Conclusion
The Expense Tracker GUI application, developed using Python and Tkinter, provides a robust solution for managing personal expenses. By focusing on user-friendly interaction and basic CRUD operations, the application meets fundamental requirements for effective expense tracking. Future development efforts can further enrich the application with advanced features to cater to diverse user needs and improve overall utility.



