Weightlifting Tracker — GUI with Tkinter and SQLite3

Editor — Ishmael Njie

DataRegressed Team
DataRegressed
6 min readJan 24, 2018

--

As of recent, I’ve been trying to learn the Python programming language; while learning the language, I’ve been having fun, playing around with different libraries and little projects I can work on; this is one of those projects.

My face may not be as comical as the one on that strong man in the image above when I am performing a deadlift, but I would not say it was far off!

But before we talk about what that guy is doing… let’s talk about the GUI (Goo-ee)

A graphical user interface (GUI) is a way of interacting with a computer visually, as opposed to the keyboard and text interaction through the command line. Features of the GUI include: scroll bars, buttons and the use of the mouse. Tkinter is the Python interface to the Tk GUI toolkit originally written in C.

Now, onto a little about what that guy is doing…! I enjoy going to the gym, however, I find it very hard to note down the weight I lift in my compound lifts each time I workout (compound lifts being: bench press, the deadlift, overhead press and back squat).

So the aim of this GUI is to make a record each time I go to the gym to illustrate the maximum weight that I’ve lifted. So how exactly are we going to store a record? SQLite3 allows for interaction with databases in Python, through SQL syntax.

Let us get right into it then!

Firstly, import relevant libraries. Tkinter to build the GUI, SQLite3 for interaction with the database and datetime to obtain correct formatting for the date. Tkinter and SQLite3 are libraries that come with the standard Python library.

Next, we will look at creating the window. The root window is created and assigned to ‘window’. The title of the root window is set and the size of the window is set. We can input text to form a header for the GUI. This is done via the ‘header’ variable.

To keep the root window open, a loop needs to be set to the root window.

Initial layout of the root window

So now we have our window, let us add some widgets. Tkinter provides list-boxes, labels (as seen in the header) and buttons (to name a few) as widgets for the GUI.

From the user, the following information is needed to store a record in the database:

  • Compound Lift: Bench, Squat, Overhead press or Deadlift
  • Day, Month and Year of workout.
  • Maximum Weight lifted on the day in Kg
  • Repetitions of that lift

Below, we will set labels for the desired information above. In order to set them in the window, we will have to apply ‘.place’ to each label.

A bit of trial and error to get the labels into the right places

Now that we have our window and our field names, let’s add some widgets so the user can input data.

First, since we know what compound lifts we want to record, we can set a dictionary for a drop down list of compound lifts.

Next, we want to create the drop down list. This is done via the ‘OptionMenu’ function. The arguments taken are the root window, the variable for the compound lift (set outside this post) and the dictionary.

We will then set the text boxes so that users can input the relevant data. Tkinter provides the ‘Entry’ function to place text boxes into the GUI.

We will then create buttons to perform actions (explained briefly in this post, the full code can be found on my Github).

Now, let’s get to the SQL syntax.

To interact with databases on a local drive, DB Browser for SQLite needs to be downloaded beforehand.

Once you have that down, we can now connect to the database, and start creating tables. To connect to a database, will create a ‘connect’ object and name our database.

The cursor will allow us to call on the ‘execute’ method, which will allow for SQL commands. The execute method will allow for a table to be created based off the selected compound lift in the drop down menu.

For the ‘submit’ button to send a record to the database, we need to isolate the text that the user will input into the entry boxes and ‘INSERT’ them into the database. Isolating the text/option is done via the ‘.get()’ method.

Given the four compound lifts we have, below, we can see all of the tables for each exercise. (OVH: Overhead press)

DB-Browser window showing all four tables.

Let us take the bench press for example, my favourite lift …

Some say I am as strong as Arnold on the 31st of February …

We can see how the records are inserted into each relevant table.

For the bench table, we can see that 7 records are stored. The date, in an appropriate format, the maximum weight in kg and the reps of that lift. This will help me measure progress.

If you paid attention, you would have seen the ‘Open DB’ button on our window. I wanted to make the GUI very easy to use and helpful. The ‘Open DB’ button will print some of the records onto the window.

Did I mention that I liked the Bench press…?

Here we can see that some of the records from the ‘Bench’ table can be seen on the window for easy viewing, as opposed to opening the .db file every time I want to view records.

So hopefully now I can track my progress better instead of having trouble trying to recall the previous weight and reps of each lift.

Thank you for reading, please leave comments regarding any questions and/or appreciation for the post :)

The full source code can be found on my Github — here there is a link to an executable (.exe) that can be downloaded to run this GUI!

--

--