 Okay, hi everybody. In this tutorial, I'm going to show you how to make a BMI calculator app using TKInter, Python 3, and a little bit of know-how. So let's get started. So first, I want to show you what we're going to be creating. So here's my here's my app, and it's a BMI calculator, and the BMI is calculated by taking your weight and kilograms and dividing it by your height and meters squared. So that's what we're doing. So let's say I am 80 kilograms. Let's say I am 1.75 meters tall, and I'm going to click the calculate button, and you can see my BMI is calculated as 26.12. So just a couple things. We've got a window here. I'm going to show you how to make a window. We've got six widgets, and widgets are gooey kind of elements. So we've got one, two, three different labels. We've got one, two, text entry widgets, and we've got one button which starts the calculations for us. So let's get started. Now something interesting. This program is going to take less than 40 lines of code, which is really really fascinating and shows I think the power of Python, and we are using, as I mentioned before, the TKInter module. This gives us the gooey elements and lets us create windows and things. Now if you are on Python 2, this would be a little bit different. I think it's a capital T, but if you're on Python 2, upgrade, update, it's free. So the first thing we need to do is to create a root window. So I'm saying root equals TKInter.tk. Note the capitalization. TKInter is lowercase, but TK is capital. It's a class name. And then we're going to give our program a title. So the root window is called root. And I didn't have to call it root. I could have called it x. I could have called it y, but it doesn't make any sense. So we're going to call that the root window. So we'll call the title is going to be BMI Calculator. And that's that. And then for gooey programs, what we need to do is to keep the window open so that it doesn't close at the end. So I'm going to go down to the bottom here. I'm going to say root.main loop. And that will keep our window open. If you don't do that, your program will open and shut just instantly. So I'm going to go ahead and run this. And you can see here, I've got my BMI Calculator program window and nothing inside of it. So let's go ahead and start making our program. So the next thing I want to do is I would like to I guess, let's see, let's start with our gooey. Let's create our gooey here. So create the gooey. So if you recall, there were a number of elements on that. So I'm going to go through and just kind of make them one at a time. We had a label and notice the names I'm using. I'm trying to use really clear names so we know exactly what everything is. So tkinter.label and I always spell that wrong. And we have to tell it where it's going to go. And it's going to go into the root window. And I want the text to equal kg colon. And that's what I want the text to look like. Now, if I run this, watch what happens. Okay, you can see that there is nothing here. So creating a gooey widget on the actual window is a two-step process. We have to do something called, in this case, we're going to use pack. So it's label kg.pack. And pack is called a geometry manager. And this manages the placement of these gooey elements. Let's go ahead and run that and see what we've got. Okay, so now it's interesting. Now you see how it's popped up really small. It's auto-sized down to the size of this particular widget. So that is our kg widget, our kilograms widget. I also have a label for the height. And let's go ahead and just do labels first quick. And so this will be label height. Let's spell that correctly. Over here, we'll change that text to height. So I'll keep it in capital letters to keep it consistent. And then I have one more label. And that is the result. Now, I don't have to label this, you know, label result, label things, but it's going to make your life a lot easier. And so this is going to be BMI at some point. It's going to give us the BMI. Let's go ahead and take a look at what we have here. Okay, so you can see I've got my three widgets, my three label widgets. I got height, kilograms, heights, and BMI. Now, so that's how you do labels. Now we also have two text entry widgets. So we've got an entry for kilograms. And that is going to be tkinter.entry. And again, we put root here, we don't need to put text or anything, just leave it like it is. Again, we need to do the, we're going to pack it. Actually, we're going to change this to something else in a little bit. But for now, we're just going to use pack. And then I also have, well, actually, let's go ahead and test it. That's what I always recommend. So you can see now I've got a place where I can enter data. So I know that's working. So I'm going to go ahead and copy that. And that's going to be height, because that's I'm entering the height here. And let's go ahead and test it. Okay, so I got kilograms, height, BMI, blah, blah, blah. And my I should say, I should say, blah, blah, I got my two text entry widgets. And finally, I need a button to calculate everything. So I'm going to make a button. I'm going to call it button calculate. Again, if you use really clear variable names like this, you'll find your program is easier to keep track of. It's easier to do things. And when you do make a mistake, it's easier to track down. So tk, enter dot, this is no surprise will be button. And I have to put again, root tell it which window it's going to, I'm going to put the text of the button. I want to say calculate. And then finally, I need to connect it to a function. So I'm going to call my function calculate BMI. And the same thing I need to do button calculate dot pack. I'm going to test this program. And we're going to get an error. And I'll explain why in a second. So you can see the error here says name error says calculate BMI is not defined. So it's looking for the calculate BMI function, which we haven't actually created. So I'm going to go ahead and I'm going to create that up here. So I'm going to say define calculate BMI. Notice I'm putting nothing in the parentheses. And I'm just going to set put pass for this is temporary. Okay, so I'm going to go ahead and run my program. Just make sure it is working. And did I spell calculate? I spelled calculate eight wrong. Okay, let's try that again. And now you can see it's working. If I click this, nothing happens, because my function is empty. So that's basically, you know, the setup of the GUI. And now the last part is pretty straightforward. We just need to actually calculate the BMI. Okay, so here's what we're going to do. So this is a way of approaching a problem. So right now we don't know how to get the information from the entry widget to our formula. But let's let's just try it this way. Watch what we're going to do. So I'm going to say kg equals, I'm just going to put some numbers in. I'm going to say height equals 1.75. I'm going to say BMI equals kg divided by height. And double asterisk means squared. Now, because of what's called operator precedence, it's automatically will work fine. But we can put this in parentheses. And watch what I do. I'm just going to go ahead and print the BMI. So this will tell us if the program is kind of working. Okay, so I'm going to go ahead and just hit calculate. Because again, we've already put the numbers in. It says watch down here, because we're going to be printing. I'm going to hit calculate. And you can see 26.12, blah, blah, blah, blah, blah, blah. Okay, now I don't want my, I don't want my BMI to have all those dots. So I'm actually going to use round, the round function. And two tells it to round it to two decimal points. Let's go ahead and run that again and test it. Again, every time you do something, you add something, it's a good idea to test it. So I'm going to hit calculate. You can see now it says 26.12. Now, program is working. But the information is not appearing where we want it to appear. It's not coming from where we want it to come from. So what I got to do is if I want to get information from this entry widget, I do entry, I do the name of it. And notice how consistent this kg equals entry kg. And I think it's dot get. So you should be able to guess what height is going to be. It's going to be entry height dot get parentheses. Now let's test that and see what happens. I do expect an error. I'll explain why. So I'm going to go ahead and hit, I'm going to type 80 in here and 1.75. So I'm going to hit calculate. And you can see here we've got some errors, which is what I expected. So it tells you type error, it tells you where. This is again, one of those things that really drives me notes about beginners is they don't read error messages. So it's telling me line 16 in the calculate BMI function tells you the exact line says unsupported operand types for asterisk or pow string and int. So what it's telling me is that I'm trying to square a string. So as you hopefully know, as you hopefully know, the see there we go. Okay, as you hopefully know, variables have types. So when I typed into that entry widget, it was automatically a string. I can't do calculations with strings. I need to convert them to in this case, a float. Because I could have 1.75, etc. float. So basically what I'm doing is I'm taking whatever text is entered into entry kg, whatever text is entered into entry height, and converting it to a float and then doing the calculation. So let's go ahead and do that. I'm going to again put in 80. And the reason I'm using the same values is because I know what the result's supposed to be. So I know that it should be 26.12. And if I'm getting the correct result, I'm happy. So the final thing is I want I don't want this to go into here. I want to go into the GUI instead of printing it. So here's what I'm going to do. Now the label that I'm using is called label result. So what I'll do is I'm going to say label result. And I need to put brackets quotation text. There's another way to do this with configure, but I'm just going to do it this way equals, I'm going to use an f string. Now if you're not using Python 3.6 or above, this won't work. If you're not just upgrade or figure out how to do strings yourself. So BMI and in parentheses, I'm going to put BMI. Actually, I want to do I can put the round here or I can actually I can just go ahead and put it here and go round round like that. And comma two. And then that should put BMI and then whatever the number is into the label. So let's test it. See if I got that right. Okay, so we've got 81.75 calculate. And there we go. We have a working BMI calculator, which is pretty cool. I said so far we only took 38 lines of code and five of those are comments 12345678910. 10 of those are spaces. So basically is 25 lines of code, which is pretty, pretty awesome. Now, if you recall, the original program that I showed you did not look like this. Okay, because we're using the pack geometry manager. What happens is it just puts them in the order that you enter them. So what I could do is I could go ahead and I said let's close that first to make this a little bit better. I could go and put these in the right order. You know, I put them in, I should say right order, but they put them in a more logical order. And this is one approach, but I'm going to show you another make it look really, really nice and happy. So I'm going to save that I'm going to run it. Okay, so you can see what I did here. I put the late kilogram label here. So I put my kilograms there, put the height label there 1.75. And I put the height text entry there and I have the button and the result will come out down here. And that looks fine. I mean the name of the app is kind of cut off. And I could leave it like this. If you like that, stick with it, do your thing, you be you. What you can do, if you do like that style, as you can say root dot geometry, and you can actually set the width and height. So maybe I could do 300 x, I don't know, sorry 100. You may have to play around the numbers. It's not high enough. So let's try 150. Okay, so there we go. So again, 80, 1.75, calculate. And here's our app. It's a perfectly functioning app. You can leave it like that. Personally, I don't like that. I'm going to get rid of this because I'm not going to need this. And you'll see what I'm going to do here. There is a second geometry manager called grid. And so here's what we're going to do with the old grid. So what grid lets us do is to put our layout or to lay out our widgets in rows and columns. So I made this little chart here for you. So you can see how this is column zero because computers like to start at zero. And this is row zero. This is column one. And row zero, row one, row two. So the KG label is going to go in column zero, row zero. The height label is going to go in column zero, row one. The button is going to go in column zero, row two. So I'm just going to go ahead and put that over here. And I'm going to put this off to the side. And so watch what I do here. Now you see how this has pack. Okay, you cannot mix pack and grid. You absolutely have to have them. You can only use one or the other. So if you mix them, nothing will appear. So I'm going to change this to grid, not grid, I'm going to change this to grid. And the KG label is column zero, column equals zero comma and row equals one, or row equals zero, excuse me. Now I'm just going to go ahead and copy that. I should copy this part probably smarter. And then the entry widget for that is in column one, row zero. It doesn't matter. I could have done rows then columns or columns and rows doesn't really matter, whatever you're comfortable with. The height label goes into column zero, row one. The height entry widget is row one, column one. The calculate button is in column zero, row two. And the BMI label is in column one, row two. So that's the result label. And that is in column one, row two. And hopefully I didn't mess anything up as I was talking. So let's see if we can squeeze this over here a little bit. And I'm going to go ahead and run it and see what happens. Fantastic. It is exactly what it should be. I am very, very happy with myself right now. Okay, so let's again, always test it. So 80 kilograms, 1.75. And the BMI is 26.12. Now at this point, I should probably check another number. And just make sure it's calculating and pulling these numbers out of here. Make sure if I change this, let's say I was shorter and heavier, my BMI would be 40. So that would not be good for your health. And that is that that is how you do a very simple GUI app using numbers and converting, you know, and converting and calculating a BMI. So I will put all of this code, I'll put a link to this code down below in the description. If you like what you saw, please subscribe. I do appreciate every subscription. And if you do that need help, click the need help link below as well. Check that out. And I will get back to you with some help when I am able to. Okay, have a great day. Keep on coding.