0:00
In the last lecture, we built the, kind of the basics of a calculator.
We had buttons that did things like print out the answer, add and subtract.
However our calculator has a severe limitation.
It only worked on fifteen and three. So we're probably not ready to start
bragging about our calculator just yet. So what we're going to do this lecture, is
I'm going to show you how to add an input field to your frame that allows you to
enter numbers that can be used inside your calculator.
And when we're done, we'll have something that even though it's still kind of old
school, it's starting to have the bare bones basics of the way a modern
calculator works. Well, let's go to it.
1:12
So a few more buttons. We can print out the contents.
We can add ''em. We can multiply ''em, divide them.
Well, it's starting to come along but we need to figure out how to actually enter
numbers now, so we're not always working on twelve and three.
So let's go look in the docks and see what super gui provides for entering in
information. So we go over in the docks and we see we
have some control objects again, so the thing we're going to enter actually is the
control object. And let's say we have buttons, key
presses, mouse clicks, but of course, here's something, text input.
Now this is not exactly what we want cuz we want to input a number, but it looks
like we don't have our number input function.
So let's just see what we can do in text input.
1:59
So, simple enough it said, add input, we give it a string that describe it, a
handler, we give it a width, that's nice. But there's something fundamentally
different between a handler for a button and a handler for an input field.
In a button, we just clicked it, and it did some action.
Here, We have an input field.
We have a number that the users typed in and then maybe hit enter and when they hit
enter. The input field handler gets called and we
need to have that handler have access to whatever the user entered.
So way this is handle is that, The input field handler is called over the
single parameter. That parameter is whatever the user
entered. So, when you implement the handler, you
are going to need to go through, and take this input parameter and do something with
it to react to whatever action the user intended when they entered that number.
So, with that kind of starting point, let's go through now and see we can piece
together how our handler for input field should work.
3:05
So, let's add an input field to our calculator code.
So really it's fairly simple. All we're going to do is we need to go
through and define a handler for our input field.
So let's say define enter, remember the handler is going to have one parameter and
that's going to be the, input called INP. And what's it need to do?
It needs to go through, and take that input, and assign it to the operand.
We want to put it in store, that's why we had swap.
So we have to be careful. There's a couple of things that could go
wrong here. Let's see,
We want to take input. We want to assign it in the operand.
So I could say something like, operand is equal to input.
And, I guess I could say output at this point.
But I made two errors here. The first thing is, remember if I want to
modify either the operand or the store, those are global variables.
So I need to go through and say global operand.
I'm gonna leave the other, other error uncorrected and we'll just see what
happens when I try to do it. Let's go through and actually kill off
twelve and three. Let's initialize them to the more rational
values of zero. And let's go down and.
4:32
Enter say F dot add input. Say enter, what the heck, enter operand.
Let's see if we can stick it, squeeze it in there.
We'll say enter and give it a width of 100.
And, hmmm, so we created our event handler.
We registered it, we reinitialized the globals.
Let's try running it and see what happens here.
I think it's going to be interesting. Okay, good, so we have enter operand-
that's nice it tells us what we need to enter.
And, well we could print over here. Things are looking good.
Not anything with 0,0, so let's enter an operand here.
So, I am going to enter twelve, my favorite number and to actually, make that
event handler trigger, I have to hit enter.
5:23
And, what do we see? The operand is twelve.
Interesting, okay, didn't expect that. Let's watt that in.
And let's enter, three and hit Enter. Boy, it's working great.
We've got twelve and three. But I think something's amiss here.
Let's do the following, let's try to add those two.
Ooh, we get 123, this is really getting good.
I'm going to try something more daring. I'm going to multiply them.
Oh, I just blew up. So what happened in there?
Well, I didn't anticipate things going quite this crazy, but I think it's
actually, you'll see kind of what went on. I said Operand is equal to input.
Now what did I tell you input wise? This was a text input.
This wasn't a number input. So input was extreme.
So whenever I said operand is equal to input.
It actual set the operand to be the string one two.
Then I swapped it, it made the store the string one two.
Then I entered three. So it said operand equal to three.
Now at that point I was expecting it to blow up.
I was thinking, oh okay, you know, we do string twelve plus a string three, what
could happen? Well, huh, it concatenated.
It made the string one, two, three. Okay.
Everything was going merrily, and then I said, hm, let's multiply.
So I said, the string one, two, three times the string three.
At that point, things blew up. Didn't know what to do with
multiplication. So, this is like more of a story why you
should do actually a fair bit of debugging, that was unplanned by the way.
I knew it was gonna, we didn't have the right type for the input, but I didn't
realize it's gonna go so far. But I think, we can actually learn more of
a story from this is, which is we wanted a number here, we wanted the operand to be a
number. This is a string, so we can fix this very
easily, all we need to do is to coerse this string into being a number.
So, we have some functions, we know how to do that well.
The function int converts it to an integer.
So, let's say, int of input. And we'll run it again.
7:38
So, let's try our favorite example again. Let's try it then twelve.
Hit enter. Operandus twelve, great.
Let's swap it. Let's enter three.
Looking good, let's add ''em. Well we got fifteen, that was better, we
didn't get 123. And then let's see, let's multiply ''em.
So now it's actually working the way we want.
Well kind of, there's still one more thing.
You notice we're only working on integers here.
Let's see if we can make this work maybe with decimals numbers, cause we really
like to use decimal numbers in our calculator.
So if I said, 3.14159, and I said enter. Threw an argument- threw an error here.
It did not want to take 3.14159 as a string and convert it directly to a
floating point number. So, let's just, we have, easy thing, let's
just make this a float. We can converse a string into a float so
I'll say float. And let's run it one more time.
We come back. Let's see, we can enter twelve.
Enter. And we can swap it.
And now let's enter, say, 3.14159. Enter that.
I notice now it's fine. In fact let's just go through and do
multiplication. So, sure enough, now actually our
calculator is working with floating point numbers so we're really doing decimals.
So we've got the basics of a calculator here, you know, we could kind of make it a
little bit nicer. Next week we'll go through and actually
print out the store and the operand here on the screen make it look like the
display of a regular calculator. But I think you can kind of see the basics
of how this works. This is a nice demonstration of how
buttons and input fields work. And I'll see you back in a second for
guess the number.