Calculator Addition Function - Python Kivy GUI Tutorial #16

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys john elder here from coding.com and in this video we're going to work on the addition function for a calculator with kivy and python all right guys like i said in this video we're going to work on the addition function for our calculator but before we get started if you like this video you want to see more like it be sure to smash the like button below subscribe to the channel give me a thumbs up for the youtube algorithm and check out cody.com where i have dozens of courses with hundreds of videos that teach you to code use coupon code youtube want to get 30 membership that's all my courses videos and books for one time fee at just 49 which is insanely cheap all right it is monday morning here in vegas the day after the long thanksgiving holiday hope you guys had a great holiday weekend if you celebrated thanksgiving if not hope you had a great holiday in this video like i said we're going to work on the addition function so we've got 99 plus one we hit equal to boom it hits 100 so we can go 9 plus 3 12 plus 56 68 whatever so that's what we're going to work on in this video so i've got our calc.pi file and calc.kv file from the last video if you didn't see that video check the link in the comment section below for the playlist and the first thing i'm going to do is head over here and anytime we press a button for instance well a number button at least we want to pass that number into a function on our in our python file and then do something with it right so let's start out with this one and i'm just going to go on underscore press like we've done before let's go root dot and let's call this button underscore press because it's very original very original name and let's just pass in seven right so this is the seven key the seven button we're just going to pass in the number seven into this function now we haven't created this function yet we'll do that in just a second so i can just copy this and we can come through here and let's just for each of these i know this is very exciting but we'll just sort of paste this in so we've got nine there's probably a faster way to do this but it's monday morning so we're just gonna kind of manually plow through this here so this is gonna be five and this will be six put a little space after each one just for fun one almost done and this one will be two and there we go this one will be it will be three and then we've got some more and let's also do zero for now and we'll leave the rest of these for later but okay we've got all these numbers now we've got this button press function we need to actually create it so let's head back over to our calc.pi file and inside of our my layout widget let's comment this and create a button pressing function right whatever so let's define press button and we want to pass in self and let's pass in a variable called button right so whenever we come over here and click on one of these for instance zero we're p we're passing in zero and that zero will be assigned to this button variable and then inside of this function we can do whatever we want with it okay so what do we want to do with it well remember our calculator when it starts there's a zero sitting in the text box at the top well if there's a zero we we probably want to get rid of that zero right it's just sort of there for visual purposes it doesn't really do anything at this point so we need to run a little if statement to say hey is there a zero there so i'm going to create a variable that contains whatever was in the text box already all right okay that is a long comment so i'm just i'm going to call this prior whatever was priorly priorly whatever was previously in there we'll assign it to this variable and so we get that by calling self dot ids dot calc underscore input dot text and you remember from the last video if we head up to the top first thing here our text input we give it an id of calc input so we're calling whatever the i whatever has the id of calc input what is the text that's in there well whatever it is assign it to prior so now let's run an if statement and let's uh determine if zero is sitting there right so let's go if prior equals zero what do we want to do well if it's zero we probably want to get rid of it we don't want the zero there most likely so we can go we could just call this thing right here so let me just copy this so we can set that equal to nothing and what else do we want to do well let's say we press the 9 button we want 9 to show up in the text box so we need to add that there so let's do this i'm going to add this as an f string and let's just pass in button so remember this button gets assigned whatever the number of the button is that we click so if we click the nine button we'll get rid of the zero and we'll add and we'll put nine in there right so let's go ahead and save this and run it just to see if that worked let's go python calc dot pi so we can say 7 boom 7 appears now it doesn't do anything else because 0 is not sitting there right so if we go back to 0 and click 3 it does something so ok that works now we need an else statement so we've got if if it equals 0 do this else what do we want to do well we want to put say we click the 9 and the 9 is sitting there but we want to say 97 so we want to click the 7 button so we the 9 is already sitting there so we need to account for that and we need to put the 7 behind it so it'll say 97 right so we can do that just by going self well we can just copy this whole thing and paste that in there let's give us some space here and inside of here we want another f string and we want to put whatever was in there prior first so we could go prior and then we also want to put whatever the button is so if nine was there already so prior will become nine this is false so this won't get run but this will and it will put nine here as prior plus then whatever we click the second time seven for instance or whatever so okay let's save this and run it see if that worked so we can say 97 or 973 or 9730. we could just keep clicking things and they keep appearing on there so okay that works good now if we keep going you'll notice boom it disappears we'll need to fix that but we'll do that much later we'll just leave that for now now let's work on the plus sign so when we've got 9 here say we want to add 3 we would click this plus sign when we do that i want the plus sign to appear right there right so then we can also hit 3 again and then 3 will appear so that's what we'll do when we click the plus sign so let's do that now so let's head over to our calc thing and let's let's look for our plus sign and i'm just going to do a quick search here so here's our plus button get rid of that now let's create an on underscore press for this guy and let's go root dot let's call this add let's create an add function right so okay we can save this head back over here and let's come in here and let's create addition function so let's define add and we also want to pass in self now when we did this button press one we also passed in the button we don't need to do that here because we're not passing in anything when we press the plus we just want to press plus right so okay that's pretty easy so again we want to do the same thing here grab whatever was previously in the text field so let's just paste that in and assign it to this prior variable right now all we want to do is take whatever was previously in there and slap a plus sign to the end of it right so we could do that just by calling this so let's say slap a plus sign [Music] to the text box whatever so we could just call that self.ids.calc dot calc underscore input.txt and we just set that equal to let's do another f string here because it's just easier that way and what do we want to do well we want to take whatever well we want to take whatever was previously in there or prior as we're calling it and then we just want to slap a plus sign on after it so we go ahead and save this let's run this guy we can go eight plus boom the plus sign appears now we can say two we can keep going plus three plus six whatever and we're good to go so now we just need to work on this equal to and likewise we could do the same thing for the minus multiplication and subtraction if we wanted to we could knock that out real quick just right here so let's go let's just look through here for each of these here's the minus one we can go on underscore press and let's call root.subtract let me just copy this and we can keep looking through here here's the multiplication so we can call root dot multiply and let's change this to a lowercase x that's kind of bugging me it looks weird and we can come up here and here's the multiplication we'll call root dot or here's the division we'll call root dot divide all right so let's go ahead and save these now just very quickly you can just sort of copy each of these we want one two three add three more so let's see this one was subtract and then down here we just wanna slap a subtract sign instead of a plus sign right this one will be multiply and again down here we want the let's put a little star here that's the shift in the 8 key on your keyboard and then finally this one will be divide and at the bottom of this guy we'll put what that division sign so okay now let's see let's go ahead and save this and run it real quick just to make sure that worked we did that kind of fast so let's go nine times three that seems to work let's go nine divided by three that seems to work let's go nine minus oh subtract let's see i misspelled subtract right there okay so there you go that seems good let's run this again just to make sure so nine minus three okay and then nine plus three oops nine plus three all right so now we want to work on this equal to sign and we're just going to work on the addition one for now i will do the rest of these in the next video but this is getting a little bit long so okay let's head back over here and let's look through here and i'm gonna just do a quick search for our equal to there it is and let's go on underscore press let's give this a root dot equals function go ahead and save this now let's come back up here and uh create equal equals to function and let's define equals we want to pass in self we don't need to pass in anything else and just like all of these things we need to know what was previously in our text box at the top right now this is going to get it interesting because this could be a long thing this could be 9 plus 2 plus 6 plus nine plus four plus 106 or it could be four plus four it could be two things but it's it's going to be a string of text with a plus sign separating different things right so we need to sort of break this apart for whatever sign is in there and like i said right now we're going to do addition so let's do addition here and now we can we can determine if addition if the addition sign is in this variable right and we can do that just by calling if and we could go the plus sign in prior right so if there's a plus sign in there what do we want to do well we need to split apart this string by the plus sign and so we can do that we can call let's create a a list a python list called number list right and this is going to equal prior dot split we want to split it apart what do we want to split it by we want to split it by the plus sign right so this will now give us a python list of all of the numbers split apart by the plus sign so let's just print this out real quick num underscore list just to see what this looks like so let's save this and run it so we've got let's say 9 plus 3 plus 2. we hit equal nothing happens but when we close this we should see a python list with 9 3 and 2 in it and we do we see a python list right here with 9 3 and 2 in it now check this out though these are quotation marks which means these aren't numbers these are strings so we're going to have to convert them into numbers into integers and if we want to do math which we do so let's go ahead and do that now so we don't need this let's create a variable called answer and let's set this equal to zero or i guess we could set it equal to zero like that so now let's create a loop so let's loop through our list so let's go four let's say let's call this number for a number in our num underscore list we want the answer to equal answer plus our number but remember this number is a string so we need to convert this to an integer let's go ahead and do that and then when this is all done let's just um let's just print out answer just to see if this is working so all right let's go ahead and save this we need we may need to fiddle with this a bit so let's bring this over let's say nine plus one equals nothing happens when we close this boom it shows us 10 down here so all right that seems to work now all we have to do is instead of printing this to the terminal we just want to print this to this thing right so let me just copy this and let's just come down here and say uh print the answer in the text box so we can set that equal to and let's just go answer now this may not work because this is now an integer so we're placing an integer in a text box so let's go ahead and run it and see if that will work or not so let's go nine plus one nope we get an error so we need to convert this into a string as i suspected so we could just go string wrap that in a string function all right now that should work let's go nine plus one boom ten and that's it so we can you know 55 plus 5 plus 10 what's that gonna be 70 boom 70 and just that easy and we can keep going 70 plus uh 10 gonna be 80 right plus six very cool and just that easy so again just to look through here when we press the equal to button we want to put whatever is previously in the text box in this variable then we want to split it apart so if it's 9 plus 3 we want to split the 9 and the 3 apart by the plus sign so we want to pull out the plus sign and put everything else into a python list so then we want to loop through this list and add the things together right when we do we assign them to this answer variable that we set to zero up here and then finally we just slap it back into the input box but remember to convert it to a string first because i guess input boxes like strings and that's all there is to it so we went through a bunch of stuff in this video but it was all pretty simple stuff you know we clicked the buttons so we're now assigning whatever was in the whatever the button is to the text box so if we press nine the nine appears now we when we press plus the plus appears and then when we press equal it adds them all up it figures out that this is a addition and uh it does stuff now for the rest of these we'll do the same thing we'll do another if statement if instead of plus you know we'll do minus right whatever now the inside stuff this stuff in here will be different for each one minus multiplication and division and we'll see why probably in the next video but uh pretty easy and we're coming right along so that's all for this video if you like to be sure to smash the like button below subscribe to the channel give me a thumbs up for the youtube algorithm and check out codeme.com where you can use coupon code youtube1 to get 30 memberships your pages 49 taxes all my courses over 47 courses hundreds of videos and the pds of all my best selling coding books doing over a hundred thousand students learning to code just like you my name is john elder from coding.com and i'll see in the next video
Info
Channel: Codemy.com
Views: 9,506
Rating: undefined out of 5
Keywords: kivy calculator addition, kivy calculator, kivy calculator add, kivy calculator tutorial, kivy calculator number buttons, kivy calculator equal button, kivy tutorial #16, john elder, john elder kivy, codemy, codemy.com, codemy kivy, kivycoders.com, kivy python app development, calculator app, john elder codemy, kivy for beginners, python kivy tutorial, build a calculator with python, kivy john elder, kivy codemy, kivy python tutorial for beginners, kivy gui, python kivy gui
Id: pdQ_KZS_GRQ
Channel Id: undefined
Length: 18min 5sec (1085 seconds)
Published: Mon Nov 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.