27 Video Views·Apr 22, 2025
In this video series we use the Python graphical user interface module known as Tkinter to generate a four function calculator! The best way to learn a practical example of programming in python is to combo it with a functional application by creating a good GUI to display the functionality!
This is Episode 2 and covers all the functions required to make our calculator fully functioning! We start by defining the function to add characters to the numerical values 0-9 plus a decimal point, then we create the function to receive an operator input, followed by the function to execute the math when the equals button is pressed, and finally the clear function to revert everything to its starting state! Tkinter GUIs require some specific syntax to update a text input or label using the StringVar() properties so we cover that here as well!
Episode 1 in the series covered defining all the numerical buttons as well as the mathematical buttons to serve as the application's inputs, and creating the text box label for displaying the operations and answers as they execute. The widgets are all defined and then placed into a grid layout, and then we show how to use tkinter's 'sticky' functionality to create an app that fills the available window and is resizable!
If you found this or anything else on the channel useful be sure to leave a like and subscribe to LeMaster tech for tons of other tutorials and exercises! Leave a comment if there is anything specific you have questions on, or anything you'd like to see on the channel in the future!
Previous video in the series:
https://www.youtube.com/watch?v=yFjtMfi-TN8
Thanks for watching and good luck with your code!
what's going on you guys welcome back to
the channel and in this youtube series
we're going to be taking a look at how
to build this four function calculator
in python using uh tkinter for your
graphical user interfaces so let's get
into it
part two in building a four function
calculator using tkinter in python
so if you didn't see episode one i
recommend you check that out that's
where we defined all of the buttons you
see on the screen and the output label
and if you don't want to follow along
with the whole series then i recommend
you at least pause this video and copy
these lines of code in because we did
define all of our buttons as widgets in
kinter and then we defined their row
location and column location on the
screens and messed around with
formatting a little bit so that it would
stretch to fill the screen
so that today we can dive into actually
adding the functionality to each of the
buttons so essentially if you think
about this calculator
the buttons 0 through 9 are really doing
the same thing they're just doing it
with one different character so we're
going to create one function for adding
0 through 9 or a decimal point to your
numerical entry and then we'll create a
second function to write whatever
operator is clicked the division
multiplication subtraction or
addition
we'll add a third function for actually
executing your math
with the equal sign
and then finally we'll do a fourth
function that'll really be the easiest
to just clear everything out we'll do
that at the end so today we're going to
be looking at those functions and let's
get rolling on them
so the first thing we need to do is we
need to add some
uh we need to add some variables
to actually execute
these functions so if you think about
what we're doing is we're updating the
numbers that are entered on the screen
but if an operator is selected we also
want to show that on the screen so that
means we're going to be mixing numbers
and characters which should mean in your
head we need this to be a string value
so we're going to have the first number
and the second number as strings and
that's what we'll use to display on the
screen but then we also need those as
either integers or floats but since i
think it'd be a more useful calculator
if we
it'll be a more useful calculator if we
have it uh
um
capable of handling decimal values so
let's go ahead and make
first float and first
number as strings and integers and then
the second thing we need to do
is we need to track whether or not an
operator has already been selected right
because that's going to tell us whether
we're modifying the first or the second
number and then we also need to keep
track of what the operator is
the only way you could get around
needing to have a dedicated string
variable for what the operator is is if
you used four different functions for
the four different operators since i
like the idea of just creating one
function for all the operations we're
going to use a string to store whatever
operator was selected
okay so these variables should allow us
to do essentially all of the math that
we need for this calculator so the next
thing to do is going to be start
defining these functions so we'll call
the one
um that's going to be you select a
number or um or the decimal point we'll
call that change and the way you
um the way you do you call a function
from a button that has a parameter
passed in you do this command equals
lambda
colon and then you call the function
so
since we're passing in to modify it
first the string variable just make sure
you put that seven in quotations
otherwise you'll need to use like the
str function to convert it once you get
in here so you could you could just pass
it in like
like this as an integer
and then you could do string conversion
in there those are both equally valid it
just save you a little bit of time if
you um just put it in quotes when you
call the function
okay but so we'll do seven first and
then we'll go ahead and just kind of
copy that uh that line and we'll put it
in all of our buttons that need that one
uh right after
but so a few things that we need to use
inside this function that i don't want
to specifically
i don't want to specifically pass in as
parameters that would just take a little
while
i'm going to call a lot of globals so
we're going to use first number we're
going to use second number
and we're going to need to check if
the operator has been selected so we'll
pass operator
clicked
in and then we'll also need to pass in
the operator string
and is that going to do it and the
output string
so the reason we're passing the operator
string in is if the operator has been
selected then we need to be displaying
on the screen the first number and the
operator that's been selected and the
second number so we need all of those
passed in
and that means that we're going to be
modifying the output string as well and
so the actual code that we're doing in
here is pretty simple we're checking if
the operator has been clicked or not
and if it hasn't then what we're doing
is we're just adding the item that was
passed in right to the first number and
then we're going to
output
string
dot set
to first number so essentially what
we're saying is you haven't picked an
operator yet so we're modifying the
first number and then that's all we're
going to display on the screen
but then if the operator has been
selected then it's going to be second
number that gets the item added to it
and then the output string is a little
bit more complicated and we talked
briefly in the first video about why
this uh this output string is not just
an equals two it's a dot set that's the
tkinter string var that's how you use a
label that gets
updated consistently throughout the
thing if if we were going to use just a
string variable that we wanted to update
on the screen over and over again you'd
have to keep overwriting the label with
a new label
and that would be really tedious and
you'd have a whole bunch of hidden
widgets behind the screen so what you do
is you use this string bar
and then you have to do dot set
throughout the
throughout the program so instead of
having default in the beginning there
we'll have it blank which is really what
you'd want from your calculator and then
this is not just going to be first
number so
after the operator has been clicked we
want the first number
plus the operator string
plus the second number
right so that's going to be showing the
actual mathematical function that we've
been punching in
and so for now i think that's it but we
will want to
let's see
so let's go ahead and run this i'll i'll
copy this so that it works for seven
eight and nine just so that we can see
that all the buttons are working and
doing different things
and right now there's nothing that's
gonna make operator click so essentially
we should just see seven eight and nine
when we run this we should see the
buttons writing onto the screen so let's
see if we did that right seven eight
nine eight nine seven very cool so
that's the first function um
let's go ahead real quick uh before i
forget i also think this calculator
would just look a little better if it
was in bold so you do to do that you
just do font equals
and then bold in quotations
bold
okay and so i'm going to go ahead and
copy
just that bold piece onto eight and nine
and then i'm going to copy our change
function instruction here
onto all the rest of the numbers real
quick
four five six
one
two three
0 and the decimal and then make sure of
course to go through
and change the character for everyone
otherwise you're going to be clicking 3
and getting a decimal
ok
so
that's pretty cool now uh the next thing
i think we should do is we should
actually update the operator
so in a similar fashion we're going to
call a function uh with a parameter but
now we'll we will call
operator
and we will
pass in the parameter of
whatever operator was selected and so we
have to define that operator function
but i'm going to go ahead and
link it up to all of our operator
buttons first
subtraction
and addition
okay
and so let's go up to where we made our
change function and let's make a
operator function
and again we can call the variable
getting passed in item it doesn't matter
so now what we want to pass in just like
before we need a few globals we'll want
operator selected operator clicked
um
and then we need the actual operator
string
and then we're going to need to pass in
the first number and the second number
so
global first number
global
second number
alrighty so um the reason we need those
numbers is we have to check whether or
not
a second number has already been entered
right because in theory
if you typed in like two plus two and
then you hit plus again the way a
calculator would work is it would first
do the execute function like the what
you do when the equal sign is pressed
and then it's gonna do
[Music]
it's gonna do like the
math of two plus two equals four and
then put your new character on the
screen after it so we want this to work
just the same way
but
let's go ahead and
put that into
code here so if second number
is not equal to blank right that means
something has been entered for the
second number
um then we're eventually we're going to
execute that's what we'll call the
equal sign function
this is what we're going to do we
haven't written it yet so for now we'll
just pass okay
this is kind of a easy way out when you
know you want
code to to work a certain way but it's
not written yet just go ahead and put it
in here but then we'll do else meaning
the second number is blank
and we will say the operator string
is going to be
equal to the item that was clicked
and actually i'm going to get rid of the
else we want this to happen whether or
not
the second number is blank so the
operator string is going to now be equal
to whatever operator was just selected
right
and then the operator clicked is going
to become true
and then the output string gets set oops
and then the output string
gets
set to have the operator string and the
um and we gotta do the dot set remember
so then the output string is going to be
the first number plus the operator
string
so essentially what we said in here once
we do the math that's going to include
the equal sign
um we're going to
if there are two numbers already entered
we're going to do the mathematical
function that was already displayed and
then we will add the operator string to
the end of it
but because we haven't done our execute
yet we're going to just leave that blank
and we'll come back and uncomment this
out later but then the remaining things
we do are we say okay now an operator
has been clicked
and then we
update the string to have whatever item
was just clicked
and then we displayed that on the screen
so let's go ahead and see what is
happening now let's say 75.4
times
six
all right so that's sweet um you can see
it started adjusting to a second number
when we uh put a character in there now
what's going to happen now is you see
when i click it it's overwriting the
character which is great but we would
ordinarily want the math to execute too
so like if right now it said 75.4
divided by whatever and then we clicked
minus we would want that function to
execute
so let's go ahead and put in the
function for the equal sign
and that as we said we're going to call
that execute and we're not going to need
to pass any parameters in there because
um execute is going to be just sort of
specific to the equal sign or we'll
embed it
inside
of
we'll embed it inside of the other
function so x acute
bad time to be dyslexic
and let's go ahead and take a look at
this function we'll make it bold we'll
make everything bold it's just a
better
calculator easier to read
all right so let's go where we've been
defining these functions and let's make
execute well i am having trouble
spelling execute today
okay
so we need a few globals we need global
first number we need
global second number
and then what we're gonna do
is actually we're gonna just
call
float first float equal to a float of
first
number and we're gonna do the same thing
for
second number
so uh i guess the way i'm looking at it
here the only time those numbers
actually need to be converted to floats
is while
we're actively executing otherwise just
displaying it is what matters so it
doesn't really need to be
defined up above i know we put them up
there you could probably clear out these
floats that we defined up there because
we're just going to use them inside the
execute
but it doesn't hurt anything to have
them up there and so now what we need to
do is we need to
check the operator string
and what it's equal to right so if it's
equal to addition what we're doing here
is we're going to have first float
plus
second float
and then we are going to set the output
string
to the result of that so we'll make
another variable here we'll call it
result
and then we're going to have
result set here um but what's kind of
interesting too
is um
we want to set the first number to be
equal to the result outside of our if
statement so i'll get into that once i'm
going to copy the code we just wrote
there for the plus and we're going to do
that three more times first subtraction
and then it'll be first minus second
and then i'm gonna go ahead and copy
that whole thing
for multiplication
and division
and make sure you use the same
characters that you called because we're
checking we are checking strings here so
um if you used like an x instead of a
a little star for multiplication just
make sure that um
that you do that same character in these
okay so that should handle all four of
our operators but then as i was saying
before what we also want to do is we
want to set first number
back equal to the result
um and since first number needs to be a
string make sure you put an str on that
um because what we're what we're doing
is we're saying well you can keep using
that number right that's super common on
a calculator you'll say i want this
number times this number and then you'll
continue going
and do something else divide it subtract
something else off of it and then let's
clear out the second number that's going
to be the second important piece because
now we're storing all the information
that's been entered so far
inside of that guy and since we're here
before we run it and make sure
everything's working let's just define
the clear function as well that's what
will happen when you click the clear
button and it's really easy we do need
to pass a few
globals in first number second number
we'll need to pass in the operator
string
and we'll need to pass in the global of
the
has the operator been clicked and then
we'll need to pass in the output string
and essentially what we're doing it's
really easy
we say operator clicked
equal to false
we set the operator
string equal to blank
we set first number equal to blank
and second number equal to blank
blank is what i call nothing and then we
do operators output string dot set and
nothing so essentially when you press
the clear button we're just getting rid
of everything
command
equals
clear
and then font equals
bold
so i think we'll see when we're running
it but i think we caught everything
there and i think what we built is a
pretty sweet calculator so let's take a
look here
all right 9.5
minus 3 is equal to 6.5 hopefully nice
let's do times
6.
oh i hit decimal six okay
um
five point nine all right let's multiply
that times eight
forty seven point two plus three
okay sweet so the equal sign is working
clear appears to be working three point
two divided by 6. and so now the last
thing we did right is we need to ch make
it to where if you hit minus at that
point it actually does the function so
remember we commented out execute if the
second number already has something in
here well now we can have that live and
we can get rid of pass
so what we said in here
there we go
that should do it okay so now if we say
like uh
9.5 9.6 plus 3
and we try to keep just rocking and
rolling with our calculator before
hitting equals
so we hit plus
there it does the math for you and
that's pretty slick now so like 12.6 my
uh plus six minus three
minus nine point five
whatever um
but you can see the uh calculator just
keeps rocking and rolling um without
having to hit the equal sign every time
so there's definitely ways you could
expand upon this you could change
formatting you could change font size
you can make things different colors um
you could definitely make that decimal
point a lot larger using like font size
40 or whatever easier to read but we
just built in you know under like a half
hour we built a fully functioning four
function calculator
and
that's pretty sweet so there's a good
introduction to python gui's graphical
user interfaces this is a good
introduction to
kinter and a lot of different basics in
python so hopefully you found it useful
if you did i really appreciate a like on
the video a subscribe to the channel it
helps me out a ton
and if you want to see anything specific
in a future video just be sure to let me
know about it in the comments below and
if you have any questions let me know
about on the comments below as well
and as always good luck with your code
and thanks for watching thanks bye