 Yeah, everybody welcome to simple calculator. This is actually simple calculator part 2 and Let me show you what I have already and what I want to do with this today Now this is a previous tutorial. I've done and I'll put a link down below if you haven't done this You do that or you can just download the code and kind of play with this But I have a very simple calendar. So 3 divided by 3 Equals 1.0. What we want to do is we want to be able to actually be able to type 3 plus 3 Etc etc etc. So we're going to talk about how to do keyboard binding in TK inter so this color is based on a Kind of tutorial or an online web page I found here at Python programming that ultra vista.org etc etc etc and I'll put a link down below So I don't want anybody to think that I come up with all these things on my own I do look at other resources because I don't know everything so but I do Yeah, I don't want to forget to give credit But sometimes I do and I do apologize to those people when I do Real quick shout-out to my members Kevin the snake 16-bit member He is the snake because he has finished his first month that is now on month number two of being a member 16-bit members Paul Yan and mode are also invaders because that is their icon for being a zero month member So their invaders snake is for one month and also 8-bit member Kim Shiyoung the invader So let's go ahead and take a look at what we got to do here So basically there's two main parts of this One is we have to do what's called keyboard binding which is where we tell the computer We tell TK inter to what to do when we press a key Now we can clarify it based on save if you press key number one press key number two get press key number three But that will require a lot more coding. So what we're gonna do is we're gonna create a key binding That deals with all keys So I'm gonna put a little function and Line of code here. So let's say here key bindings First one we do is I'm gonna create a function called def key handler. You'll see this a lot The word handler. So a handler handles some sort of event. So in this case It is pressing a key and for now I'm just gonna put pass and And so that's the function that's gonna be called every time we press a key And then I have to do the binding and so that is root So put yeah, so that's root and root comes from up here where we created the root window Again, if you haven't followed this tutorial, you're not sure how this works. You might want to watch that first Yeah, so this was actually a request by Kevin the snake But then he said he didn't he wasn't he wasn't that into it, but I just went ahead and do it anyway So roots dots bind parentheses quote Key check watch the part watch the capitalization. I think it's pretty important And so what that means is any key now if I change this to I think like a Or maybe maybe like a it would do only capital a or small a like that But I want to do key if I want to do the return key It would be like return, but I want to do all keys and you'll see why in a second And so we say which key or keys are we binding and we want to what E what method do we want to call? So this is key handler and remember with TK enter. We don't put parentheses here Now you've probably noticed that there is something here in the parentheses even though we didn't put anything here This bind method by default will send some type of event To that function when we call it so what I'm gonna do here is I'm gonna put print Event dot key sim and that means key symbol This is the internal symbol that TK enter uses if I find a link I'll put that down below too if I forget let me know so let's go ahead and run that Okay, so notice no errors good sign So let's go ahead three that's working Okay, so now I'm gonna just start typing some keys and watch down here in the terminal Okay, so that was the backslash to ABCD one two three four plus Notice how the plus came out as plus not plus the sign equal minus So we always just hit slash and what's time so the asterisk Okay, so we got shift L and we got asterisk out of that so we need to account for this In our program. Okay, so basically numbers work as expected See backspace if we want to add deletions Return is the return key for calculating and see here parentheses right parentheses left So you see we got a deal with all these different Cases where we're getting not the symbol, but we're actually getting the key sim. So let's go ahead and And Stop that sorry, let's bring that back now. I am actually out in the countryside I usually live in Tokyo, but I'm out the countryside So I don't have all my fancy effects and transitions working. So I apologize for that. So What we want to do is start out Let's deal with numbers because they're the easiest so if event dot event dot dot key sim In I'm just gonna make a list of numbers So I think it's gonna be I think it's gonna be a string. I think it's gonna return a string We'll find out in a second six seven eight nine And zero So if it's one of those numbers Okay, so look up here. So you see how we added a value So this is the function that we use to add Symbols to what was going on. So I'm gonna go ahead and use that function. So if it's one of the numbers I'm gonna say add event dot key Okay, so let's go ahead and see if that works Notice how I'm testing it piece by piece. I'm not kind of trying to do the whole thing at once Which all my students like to do so I want to do two. Okay, so two works and I hit slash two divided by three And then do equals Okay, it works numbers definitely work So let's go ahead and deal with the other situations. So if event dot key sim Equals we have we had add then add plus Let's do L L L if to make it a little more efficient L if event dot So we're just dealing with each of these as we go Is it minus? Let's see what happens here. Add minus L if event key sim Was it equals I think was asterisk and that's gonna be add an asterisk That's got quotation marks L if Event dot key sim It was slash I believe slash Add Slash Okay, let's go ahead and try that see what happens So I've added the symbols so I'm just gonna type two plus two Okay, two and two it works Okay, so something's going wrong, so I'm gonna go ahead and just print event dot key sim and see what happens here so I'm gonna type two Plus it's not add it's plus okay Minus I got minus okay, so this should be not add it should be plus Okay, so I'm gonna leave that code in there for now. Just while I'm testing this I don't make any more mistakes stupid mistakes like that or make fewer stupid mistakes like that so two plus two Okay, that's working You know hit equals And let's see minus Two equals to Plus plus and we gotta do divide What divided by four? equals three point five Okay, so it's pretty much working how we expect it to work Now oops now. We still got a few other keys deal with when we pull those back up And we got see we got dot and we got equals Okay, so equals is gonna be basically hitting the return key Okay, so when we hit equals that's when we want to Calculate We also got a clear function when we hit C So let's let's go ahead do clear and that's pretty easy. So LF event dot key sim equals C Try to do small C How do we do in case it's case they've got You know capital letters Caps lock, okay, so add Let's not add it's clear Because I went ahead and try that Yeah, I'm just kind of testing it as I go along. So two plus two Equals and I'm gonna hit C and you can see it is now cleared I'm pretty happy with this now Equals there's nothing And the final one is L if I think it's the final one event dot key sim equals return and In that case, this is when we're going to go ahead and calculate Because that is the function up here that we created to do that. So let's go ahead and try it Okay, so one plus one Enter two Plus two Now I do have an equals key. I think why don't we go ahead and just do the equal key as well so if we hit enter or return or Equals we'll calculate so in Return or equal. Let's go ahead and try that So one plus one Minus two oops Ooh, I didn't deal with backspace times. It was good. Okay. Let's go ahead and deal with backspace We got backspace so L if event dot Event dot key sim Equals was backspace of note the capitalization What do I got to do here? We got to make we got to remove one item from the Let's see. What is it the label and the expression Okay, so we're using expression. Okay, I got it. So we got to remove the last letter from expression. So what we do is we say To do global up here. I know I shouldn't use global variables, but I'm gonna go ahead and do it anyway Expression it's easier sometimes so again, if I'm assuming you've just watched the The previous video so you have to review that one for kind of what's going on. So expression equals expression And what we do is I'm gonna slice it So from the 0th character to the length of expression Minus one What that'll do is it'll chop off the last letter. There's probably an easier way to do this But that's that's the one we do And then we need to update the label result and that should do it unmatched parenthesis Okay, our parenthesis there, sorry Okay, so let's go ahead and try this two plus two Equals four fantastic Times four This is ten and I'm gonna go ahead and hit Backspace, okay, so you can see it's kind of working. The only thing is the expression isn't changing how it's supposed to so Let me see here. This may be a slightly different version of the live one. I did that would be a bummer Okay, yeah, I forgot to put Expression equals result. So yeah, so Expression equals result So that will Reset that back. So let's go ahead and check this one more time. It's the old one still running So two plus two enter gives us four backspace Object int has no length length of expression Expression equals result. Okay equals string result. I'm gonna be careful with our Types, let's go ahead and try it again. So two plus two Equals actually hit enter Backspace nice five times five enter Enter just evaluates Times five. I think it's working. Oh, I didn't do a period Period. Okay, so that should be an easy one So miss that one. Yeah, I'll put that up here I'll put that here since it's kind of part of this section Now if event dot key sim Equals period And then so we're gonna add period Okay, let's test it So I'll say two point two times 3.4 enter 7.48. I'm gonna assume that's correct and then I can go ahead and delete If I hit an extra delete, I'm gonna fade there. Nope. Fantastic five plus five Okay, so now it is working. It's basically it's a You can do either keyboard or you can do the keys. Let me just go over that real quick So basically what we did Well, we had to fix that little expression thing there, but beyond that We added a function called a key handler and that handles all of our key presses and by default it sends an event and To get the key press out of that whatever key that we've pressed we use event dot key sim Okay, and if it's a 1 2 3 4 5 7 8 or 9 or 0 we add that to The expression if we get plus we add the plus symbol minus minus symbol asterisk Asterisk symbol and that's for multiplication slash the slash symbol and if it's a small c or capital C We clear the the expression and then if it's a period we add the period and if it's return or equal We actually calculate because that's what we want to happen and if we do backspace We just lop off the last character of the expression and update the label result and the magical command that Let's that makes that all happen is root dot bind key and Key handler the name of the function. So at voila again As I do with most of my tutorials or most of what I do There's a lot of googling especially stack overflow is is my go-to This one happened to be python programming dot ultra vista dot org so check out that website for more So thanks for watching keep on coding