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 may 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.
Libraries allow us to reuse code. There’s usuallya library for most thingsyou 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:
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 bychecking the Python version. I’ve already coveredhow 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.
I chose Python for this project because it’s one of theeasiest 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.
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 may 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 alotof 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.
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’re able to 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:
The final application should currently look something like this:
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.RealPythoncan 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 may 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.