Hello and welcome to the.
Part 3 of creatine a calculator with Python before we carry on.
From where we stop I just want us to quickly look at the code we have so far.
So he started off creating a class.
Then we have a few functions here created a functions called on to score in it and we initialized the
frame.
So the master here basically is the main window of the application we had created a class called widgets.
And here we have the different buttons created as widgets.
So most of the code within the buttons are the same.
The E course to button this is the coastal button here.
Here we have where we've got these sticky said this ticket equals W..
We've got a column span to two.
Normally when a widget occupies one cell in the grid you can also grab multiple cells of a row and merge
them into one largest cell by setting the column span.
Option two equals to the number of cells.
So we've used the column span here and also here.
We've used a command.
Notice we didn't use lambda in there.
Here we've used a command.
And we've said that to serve the glad display what this would do.
When you click on the color button it will clear the display on the screen.
So that's what that will do.
Also here we used this self dot task.
We've set that to blank.
Basically a task is a subclass of future of a future a future basically is an object that is supposed
to have a result in the future.
And the task is a subclass of that future.
So let's go ahead and create a few more methods of func during the creation of the buttons we used we
attached it to the bottom the click method which we haven't created some have now created it here and
the button click method here.
So it takes in the Self Variable hand the number that the user inputs into the calculator.
So here this is the logic.
So we're doing the task itself who's setting that to as TR dot.
So this here is a special string glass and steel and inside the pants parameters here.
We've caught inside the parentheses the self the task plus these SDR and then the number as Cha is a
special string function class.
Okay.
And here again we're using the user dot without using the Self Variable dot the user in how we are setting
that to this self dot task.
So this is for the button click methods when they click on the button it will kick off this logic inside
this code here.
So take note that this is t r all we have use is a python built in string class.
This is string class which is a built in function.
Sorry built in class in Python it's a built in class called S T R string built in string class chord
s t o I've created on another function here called calculate task again a past in a self variable here.
And this function I've also attached some error handling Python allows you to implement arrows add exceptions
to be handled by a program who allows your programs to handle arrows.
An exception in order to do that you need to use a try and the accept statement.
So here is my try statement and this is what I specified and here is the statement for my except so
this try and accept there to handle any errors in the application.
So basically what this is doing here.
This function here any data that is passed into that variable we get is going to be the.
Are you going to be any input so it's taking the input is getting the input from the user and then passing
it into this variable here and it'll catch any errors here.
Using this try.
So if the answer if the answer evaluates 2 equals 2 that a tool to display it does not.
If there is an error the error will be captured.
Okay so this here is used to display the text of the answer.
This will this also self-taught answer we display and the task has been imported.
And here it's a syntax that will capture any invalid syntax you enter or any error messages to be captured.
I've added two other functions here.
This one here called display text and passed it.
The self and then the value that the user inputs.
So again this is what it will do.
You take in the input from the user and if you decide to delete the little entry these are the parameters
it would take.
And if you're inserting it.
This is a code that will handle the insert.
Again this is a function.
This here this function is responsible for displaying the text.
This function here is responsible for clearing the display.
OK so any input you enter here you can clear it using this piece of code there.
So here and the bottom here whether this here.
So we sat in the calculator so equals to this TKD method which we specified earlier on the T.K. route.
So we are here both with this title.
We using that to set the title for the calculator.
And this is what we'll call it.
Okay.
This T.K. here is the route.
So we're setting up the route of the application will be the calculator and app.
The app here is refers to the application name.
This is the name of the application.
It's called calculator this here you use that to make the window a fixed window so the window cannot
be resized.
OK so hands up setting the height to force so you cannot resize the window of the calculator.
Because of this line of code here and this is the main loop of the calculator.
This is the event loop.
This is what keeps the calculator running on to you exit the calculator.
So that should be it.
I'm presuming a click save.
So let's do a quick review of our code.
Make sure the code is also formatted properly.
If your code is not formatted properly and you try to run it it can cause syntax errors and the editor
will complain.
So make sure you've got it structured the way I've structured might have also added some comments here.
So anything you see here in green here these are comments.
When you run the application the comments are ignored.
OK.
So these are comments.
And also this here is a comment.
Anyway you see the hash symbol is also a comment.
OK so these are comments.
It just helps give you a description of what we've done here.
So this tells you here that this is the main class for the calculator.
It tells you Here we are the initialize in the frame here.
We're creating all the buttons for the calculator here.
The user input we store in them as an entry widget.
Here is the button for value.
Number seven in here we've got value.
Number eight.
So these are our comments.
Anything it's easy a pound sign is a comment.
And so on so I've commented out just put a comment before each line.
So you know what they do.
OK.
So make sure your code is formatted this way.
In here there is a format option to endangered code.
See if you notice knowing that properly.
Make sure you do that so that the editor doesn't complaints you notice within the functions you see
where the function is created and then the other actions the logic inside the function.
There's indentation you can see that even with a class you can see the class the indentation from the
class and the methods.
If I scroll down you can see the indentation also.
Okay.
Here eyes all indented or the logic.
It's indented underneath the respective methods.
OK.
So make sure is horse all sorted and structured in this way.
So once you've done that you just click and save and let's try and run the application so click Run
and to run module hopefully.
Yep.
Okay that's good.
So this is the application here.
Let's see if we can do some calculation.
So that's eight times eight 64.
We'll clear that nine plus nine 18 will clear that five fifty six minus eight.
We'll clear that so all the one of we know use.
Yes to a division we do hate to divide by six.
Okay.
Clear that nine divide by three.
Can I that.
So we've got all the buttons and the functionality working properly.
So this concludes the series on creating a calculator with Python.
Thank you for watching and bye for now.