Breaking into the industry: My Journey to becoming a Software Engineer #5 — Calculator

M E
3 min readJul 7, 2021

A very common beginner project is the infamous Calculator. So without further ado, I created a GUI Calculator and this is how I did it:

Fist off, I set up the GUI of the Calculator. This was a very tedious process because when I was making the widgets, I had to make sure I entered the arguments correctly. I had to play around with the width and height so that all my widgets would fit perfectly in my window.

Snippet of my GUI set up.

Secondly, I created the number functions (e.g. def one(), def two(), def three() and etc). I created these functions so when the user clicks on one of the number buttons, I could use the number function as the command parameter and would display on the entry screen/label. I have also created the operator functions (def add(), def subtract(), def multiply() and def divide()) so that I use could these functions as the command parameter whenever the user clicks on one of the operators buttons.

Snippet of one of the Number Functions.

When a user clicks on one of the number buttons using the GUI, that specific ‘number’ function will be passed as the parameter in the “command argument”from tkinter. First, it will check if there are items in the global num variable, if so, that specific number will be appended and would update the entry screen in the GUI to show that specific number along with the other items in the global num. Else, that specific number would just be appended to the global num.

calculation() function part 1.

Calculation Function:

When the user clicks the ‘=’ button, the calculation() function will be called.The calculation function will access the global num variable, then it would split the num variable until an operator is found and update the num into a list instead of a string from (e.g. “5+5” to [5,5]). Then the appropriate operator willbe compared and then it will calculate the total. It will then display the total on the entry screen.

calculation() function part 2.

The current total will be saved in the num while num1 and num2 values will be change back to None. So the current totalwhich is now the new num variable will be used as one of the operands.

Snippet of one of the Operator Functions

Operator Functions

When the user clicks on one of the ‘operator symbols’, the operator functions will check if there are two operands in between the operator symbol (e.g. 5+5), if so, then calculation() function will be automatically called to calculate the solution without clicking the ‘=’ button and would display the answer on the entry screen.

Else, it will append the operation symbol to the global num variable and will continuously check whether an operation is in between two operands whenever one of the operation symbols is clicked by the user and then call the calculate().

Please check out my GitHub account to access the full code. :)

--

--