Yahoo
Advertisement
Advertisement
Advertisement
Advertisement

Learn Python Basics: Let's Build A Simple Expense Tracker

Shutterstock"">

A piggy bank, a calculator, and several coins coming out from inside the Python logo.

Lucas Gouveia / How-To Geek | Boris Ipatov / Shutterstock

If you've ever tried building a budget, you know how difficult it can be to get an expense tracker that does what you want it to do. But how about building one yourself? Let's learn Python basics by creating a simple expense tracker that you can actually use.

Defining Requirements of our Expense Tracking App

When I came up with this idea, I wanted to create an app that's more than just another command-line terminal app. We've built a few of those ( like the simple To-Do List using Python ). For this one, I wanted to use some GUI, so I decided we'll be importing the Tkinter library to have some usable UI elements.

Advertisement
Advertisement

Libraries allow us to reuse code. There's usually a library for most things you might want to do in Python. Importing them avoids rewriting all the code they contain from scratch.

In this app, we're going to implement:

  • Expense entry

  • Defining a budget

  • Comparing our expenses to our budget

  • View spending history

At the end of this, we'll have a decent idea of how Python works and be able to complete our first GUI-based Python app.

Setting Up Our Project

You should check to ensure that your device has Python installed by checking the Python version . I've already covered how to link my favorite IDE (Visual Studio) to Python . Once you've gotten through the overhead of installing Python on your device and getting it updated to the current version, we can start by creating a new project.

Advertisement
Advertisement

I chose Python for this project because it's one of the easiest languages for beginners . Let's dive into building our expense tracker!

Building the Main Application Window

Since we're going to be moving away from the terminal in this application, we'll instead need to set up Tkinter to build out our application's graphical user interface (GUI). UI design in Python can be complex, so I'll keep it simple and just tell you what the code I'm giving you does. Here's how my UI window is going to be defined. We'll start by importing Tkinter:

Tkinter includes all the functions we need to create a basic UI. You'll notice that we're also importing Ttk, a theming engine inside Tkinter. This allows us to control how our UI looks if we wish. We're also going to use messagebox and simpledialog to prompt the user for the initial budget setting. We also want to be able to save monthly data in case we want to access it in the future, so we'll also add at the top of our file:

Aside from being very close to my own name, JSON is actually quite cool as a serialization system. Luckily, it comes standard with Python, so we can import it directly. The datetime import helps us to keep our dates formatted properly across the whole application.

Advertisement
Advertisement

Now that we've figured out the imports let's actually build the window:

These entries are just styling our window. If you like, you can play around with the numbers and see what you get since fiddling around with them is harmless (aside from making the final window look terrible). We also need to display frame and the save button, and configure the grid so that it looks nice:

As you can see, the UI takes a lot of coding to get right. However, since you're planning to learn Python basics, understanding UI design is a part of the whole. Now that we've got the UI up and running, let's get working on the functionality.

Defining Categories and Adding Expenses

To give our app a bit of "body," I predefined a few expense categories. I included those as headers along with the rest of our definitions:

Definitions like these should be at the top of our program. To that end, this snippet should come before the widget creation code we have already developed.

Advertisement
Advertisement

The get_initial_budget(self) function creates a popup at the start of the app's run to collect data determining the amount of money you have available. Failing to enter a value causes the app to shut down immediately.

We also need functionality to add expenses and update categories with new, custom ones. Luckily, this is also simple to implement:

To round out our basic functionality, we'll need functions to update the expense list when we enter a new expense. We'll need a helper function to clear the combo box after we've entered the data. And finally, we'll need to calculate how much of our budget we still have available. This simple code snippet will allow us to do just that:

So, after we enter all this data, how do we ensure we don't lose it? I've implemented a simple save function using the JSON format we imported earlier:

This function serializes the data and saves it in a particular file location with the current date appended to the filename. The final thing we have to do is put together the main() function loop:

That should allow our expense tracker to run without any issues. Congratulations on completing your first GUI-based application in Python!

Additions, Shortcomings, and Customization

After completing this, you understand a little bit about how Tkinter works and how you can use it for GUI creation. If you know a bit of CSS or some graphic design, you should be able to figure out how to use Tkinter quickly. If you'd like a few challenges, you could try these things next:

Advertisement
Advertisement
  • Build or use a new theme for your app.

  • Create a "load" function and button for it. I specifically left out the load function to give you more room to explore.

  • Explore how you could do a graphically-based breakdown of your budget as a pie chart .

The final application should currently look something like this:

Basic Expense App

Where To Go From Here?

This development tutorial only gave you a broad overview of Tkinter, but if you're planning on doing more GUI-based apps, you'll have to get a better grasp of what it can do. RealPython can give you an idea of how to use Tkinter or any of the other available GUI frameworks or toolkits. Python is pretty easy to grasp at a base level, but there's so much you can explore as you learn Python basics. The only thing limiting what you can create is your imagination.

If you're getting stuck on the code and want my full Python script to compare to yours, it's available on my GitHub .

Advertisement
Advertisement
Mobilize your Website
View Site in Mobile | Classic
Share by: