22 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 1 in the series and covers 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!
Episode 2 will begin covering the actual operations that execute when clicking the different buttons and performing the mathematical functions!
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!
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
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
okay so the idea when you're building an
app in uh kinter is that is a module
inside of python so it's just a package
that comes pre-built you have to import
it at the beginning of your function but
that's going to give you a lot of tools
to actually display a graphical user
interface for all of your code so you
start by importing it call your
root screen something i always call it
root um and then
kind of just the bare minimum to get
something opening
you go down to the bottom and you do
root dot
main loop and uh i i like to set a title
as well otherwise the title will just
say tk but um i do it like this
root.title and then for this one it's
calculator
and let's take a look unless i'm
mistaken that's all we need to enter
just to get it uh up and running
so yeah you can see we get this window
it's resizable it's kind of in its
default size when it opens up there
our title of calculator is on there but
it's not doing anything yet we don't
have anything on it so let's go ahead
and take a look at that
um so i have this snip here of what the
calculator is going to look like it's
essentially five rows right in four
columns
and remember the numbering starts at
zero for programming so it goes zero one
two three and then zero one two three
four for the rows
and that's really important that we kind
of have the grid pictured there because
um
the tkinter like way that you define
where a widget goes
is a grid system so
with that in mind let's start drawing
our buttons
um the way you create a button
is so let's say
define our buttons
um it's a pretty standard gui practice
actually you define the the widgets in
one spot and then you draw them onto the
screen in a second spot and that's the
same with tkinter it's the same with
kivy it's actually the same with pygame
as well so for the buttons we're just
going to start in the top left corner so
you'll remember from our screenshot i'll
click back here occasionally that's
going to be the 7 button and so 7 button
equals and then capital b button that's
how you call the uh tkinter or kinter
button format and we're just gonna put
on the root and we're gonna give it a
text of seven for now and that's all
we'll do per button so we've obviously
got uh you know one two three four
8 12 16 17 buttons and a label real
quick so i'll go ahead and
copy paste and kind of fly through
doing that same thing
i do think and we'll kind of see why
when we get to the second part here
but i think doing it in an order that's
kind of seven eight nine division four
five six multiplication one two three
subtraction
is going to be what we want to do
because
so we'll call it divide button um
because when we copy it into the next
section that's where we're actually
going to be giving it grid coordinates
and um
that is
uh it's just a lot easier to see right
away like what you have in each
row and column if they're all
defined in more or less the same
location
all right so four five
six let's get our
multiplication button
um multiply
and let's spell it right okay and then
we need all these again actually
so now four five six we've got one
two
three
what else uh we've got
subtraction in that spot
and then underneath it we've got zero
i think we had
decimal
then we had the equal sign and then we
had the plus
if you're following along and you want
to build this in your own way you can
certainly play around with the
what buttons you put where you know
that's actually great practice as a
exercise for yourself is just
say well i don't want subtraction above
edition so i'll flip those
or play around the layout any way you
want that's great practice
for me i did it just fairly similar to
what the little calculator widget that
pops up if you hit the calc button on
your laptop that's kind of the format i
went for just so you guys know where i'm
coming from
but uh okay so then uh we're now into so
i'll go ahead and do the clear button um
because that's still clr but uh we we do
need to do something different for the
output right the the actual spot
that the numbers are going to appear and
then the answer
we're going to call that a label so
that's output label
equals label and then you still put on
the root and you still give it a text
but since this label is something that
we want to be uh having change every
time you click a button
you're actually not going to define
static text you're going to use
something different called string
text variable
and we will
we will give it a variable that we have
to go and make called output string and
then you notice we have it as black and
the default button color is this kind of
gray
so after this we go over here and we do
bg which is bla background and then we
define black right there and then um we
want the text because the text defaults
to black and that's the foreground or fg
we'll have that be white so that'll
really pop
and now we need to come up here and
define the output
uh string and there's something special
you have to do in tkinter when you're
defining a string variable that's going
to be updating inside of a label
constantly
you create this variable and you call it
a string var just like that
and then after that you're actually not
going to do the like standard python
method of saying output string equals
whatever you use this output string dot
set and so for now we'll just throw
default in there so it'll appear
but uh okay so we defined 789 division
456 multiplication one two three
subtraction zero decimal point equals
plus and then our label and then our
clear button and that
should be uh great for the next section
now so let's go ahead and grab this
whole define button section and now we
are going to actually
draw onto grid
so we'll say draw buttons
onto grid comments are a great way to uh
to keep your code organized and clean
and so now we with the name of whatever
you named the button if you didn't name
it exactly like i did here that's fine
but just make sure whatever you did name
your button you do button name dot grid
and then in here we're going to give it
two variables we're going to give it rho
equals and these actually don't need to
be strings row equals zero
column equals zero
okay and now i'm going to copy this dot
grid that's going to be a lot faster
than typing it everywhere and now you'll
also see why it was really easy to put
them in this order because as i go
through
i can just tweak it a little bit
you know zero one two three it's really
easy to keep track of and then for the
four button we're on to row one and then
column zero
and we're just going to go zero one two
three zero one two three and once we get
to if you're thinking ahead a little bit
to the output label um you probably
noticed it it does stretch across three
um columns so if you're unfamiliar with
sorry i'm trying to
kind of get ahead of myself with the
tutorial here but if you're unfamiliar
with kinter and you haven't seen the
column span function before then that'll
be that'll be a great little
intro for you
because there's a function called column
span
that allows you to set a widget
you define where it starts but then you
also say how many columns you want to
stretch across
already almost there row three column
two
and
row three column three
[Music]
okay and now let's go ahead and just uh
do the clear button while we're here
that's gonna be row four
column three and then the output label
is going to be row four and we're gonna
put it in column zero that's where we
want to start but we use this other
variable
column span and we want to stretch
across three columns right so look it
starts at zero but that stretches one
and two
okay so i believe we've done everything
we need for all the buttons we want in
our calculator to be on the screen just
with these two sections we defined them
and then we said where they're getting
drawn onto we haven't done the
functional piece of it yet
but we have defined them all so let's go
ahead and run this and what you'll see
is the stretching oh i s
it looks like we put
we yeah yeah we did we left the output
label in row three let's put it in row
four
okay and what you're gonna see is it's
just this very strange format it doesn't
resize when you uh widen it or anything
and every widget is as small as it can
possibly be and it doesn't stretch to
fill the space so the last thing we'll
do in this video now
we're going to format each of these so
that it does stretch to fill the space
and
there's one little weird thing you have
to do before you can
stretch those buttons you have to give
each button what's called weight and
it's just kind of a it's kind of a
kinter um
it's kind of a kinder unique
thing is is before you can
before you can say all right i want this
row to stretch i want this column to
stretch i want this widget to fill the
space you just have to go in here and do
a function that's called grid dot
configure
sorry it's column configure and row
configure so both of these need to get
done but you'll see i put it in a for
loop because we would have to do this
for every
row and column
we would have to say for row 1 we want
to give it some weight and essentially
the default weight is zero and what i'm
doing here is i'm just giving them a
weight of one i don't actually know what
doing like a decimal would do it would
probably make everything like
proportionate but i've always found it's
easiest to just give them a weight of
one and
now just looking at this i did it this
way because uh
i did this way because we have zero
through three on both of them and that's
what this will do but we also have row
four so i'm just gonna come down it's a
little janky and you could just tweak
your
you could tweak your loop to catch uh
row four as well but i'm going to leave
it here and say um this so this for loop
is giving weight to all of our columns
in row zero through three and then this
additional row is just giving weight to
uh row four and i'll show why i did that
in a second
but what we really want to do now is we
go into
the grid definition
and there's one more thing we're going
to do
and that's called sticky and you're
essentially you have the four cardinal
directions northeast south and west
and
you have the actually it's lowercase um
but
you're saying whatever you put in that
spot you want it to either stretch to
the left stretch to the right stretch up
or stretch down to fill the available
space inside of its widget
and so since we want every widget
the way the calculator works it really
fills the screen widget to widget so we
are going to do sticky in all four
directions for every widget now let's go
ahead and run that
and you'll see immediately it resizes to
fill the screen it looks great um none
of the buttons do anything yet because
we haven't given them command functions
but we're working on getting it drawn on
the screen so far you can see just with
that sticky command
it resizes really well now let's go
ahead and look at why you need to define
weight for each thing i'm going to
comment out row 4 real quick so you'll
see what happens when you don't give row
4 weight
actually
so you see it's not the same size as the
rest of them
so that's okay you can play around with
the waiting if you want but if you want
all of your buttons all of your widgets
to have the approximately same height
and width then it's important to give
them all weight you can see the clear
button now is
is uh the same height as the rest of the
buttons the label box is the same height
and when we didn't have weight on that
row 4 there
it was just the minimum size it needed
to be for the text to fit but it didn't
get any bigger than that as we resized
our screen so there are applications
where you'll want just one row of
widgets that don't resize
but for now we're going to
keep it that way
and we just drew the entire
application that we'll be using in one
video in 15 minutes so great job
hopefully you found this useful and
informative if you did be sure to like
and subscribe and check out the channel
for more content and if you have any
questions about what you saw here today
or anything you want to see in the
future just be sure to let me know about
in the comments and i'll get back to you
as soon as possible and tomorrow we'll
go ahead and take a look at actually
adding the functionality to each of
these buttons and operators to actually
make this a functioning calculator so i
hope you found this useful and
as always good luck with your code and
thanks for watching thanks bye