1 00:00:00,480 --> 00:00:02,170 Hello and welcome to the. 2 00:00:02,200 --> 00:00:08,820 Part 3 of creatine a calculator with Python before we carry on. 3 00:00:08,820 --> 00:00:14,010 From where we stop I just want us to quickly look at the code we have so far. 4 00:00:15,030 --> 00:00:18,570 So he started off creating a class. 5 00:00:18,570 --> 00:00:26,340 Then we have a few functions here created a functions called on to score in it and we initialized the 6 00:00:27,480 --> 00:00:27,990 frame. 7 00:00:29,280 --> 00:00:41,190 So the master here basically is the main window of the application we had created a class called widgets. 8 00:00:41,190 --> 00:00:46,920 And here we have the different buttons created as widgets. 9 00:00:46,950 --> 00:00:51,240 So most of the code within the buttons are the same. 10 00:00:51,240 --> 00:00:57,040 The E course to button this is the coastal button here. 11 00:00:57,090 --> 00:01:03,270 Here we have where we've got these sticky said this ticket equals W.. 12 00:01:03,480 --> 00:01:07,350 We've got a column span to two. 13 00:01:07,620 --> 00:01:18,090 Normally when a widget occupies one cell in the grid you can also grab multiple cells of a row and merge 14 00:01:18,090 --> 00:01:22,970 them into one largest cell by setting the column span. 15 00:01:22,980 --> 00:01:27,330 Option two equals to the number of cells. 16 00:01:27,360 --> 00:01:32,070 So we've used the column span here and also here. 17 00:01:32,070 --> 00:01:34,370 We've used a command. 18 00:01:34,530 --> 00:01:37,480 Notice we didn't use lambda in there. 19 00:01:37,740 --> 00:01:39,490 Here we've used a command. 20 00:01:39,720 --> 00:01:43,450 And we've said that to serve the glad display what this would do. 21 00:01:43,680 --> 00:01:49,560 When you click on the color button it will clear the display on the screen. 22 00:01:49,560 --> 00:01:52,310 So that's what that will do. 23 00:01:52,320 --> 00:01:56,690 Also here we used this self dot task. 24 00:01:56,850 --> 00:01:59,620 We've set that to blank. 25 00:01:59,640 --> 00:02:11,760 Basically a task is a subclass of future of a future a future basically is an object that is supposed 26 00:02:11,760 --> 00:02:15,240 to have a result in the future. 27 00:02:15,240 --> 00:02:19,080 And the task is a subclass of that future. 28 00:02:19,200 --> 00:02:28,660 So let's go ahead and create a few more methods of func during the creation of the buttons we used we 29 00:02:28,660 --> 00:02:35,770 attached it to the bottom the click method which we haven't created some have now created it here and 30 00:02:35,770 --> 00:02:37,400 the button click method here. 31 00:02:37,420 --> 00:02:45,730 So it takes in the Self Variable hand the number that the user inputs into the calculator. 32 00:02:45,730 --> 00:02:47,780 So here this is the logic. 33 00:02:47,890 --> 00:02:54,280 So we're doing the task itself who's setting that to as TR dot. 34 00:02:54,290 --> 00:03:02,170 So this here is a special string glass and steel and inside the pants parameters here. 35 00:03:02,170 --> 00:03:10,930 We've caught inside the parentheses the self the task plus these SDR and then the number as Cha is a 36 00:03:10,930 --> 00:03:13,990 special string function class. 37 00:03:14,340 --> 00:03:14,730 Okay. 38 00:03:14,740 --> 00:03:23,260 And here again we're using the user dot without using the Self Variable dot the user in how we are setting 39 00:03:23,350 --> 00:03:27,670 that to this self dot task. 40 00:03:27,850 --> 00:03:38,140 So this is for the button click methods when they click on the button it will kick off this logic inside 41 00:03:38,140 --> 00:03:41,930 this code here. 42 00:03:42,520 --> 00:03:50,960 So take note that this is t r all we have use is a python built in string class. 43 00:03:50,970 --> 00:03:55,080 This is string class which is a built in function. 44 00:03:55,150 --> 00:04:06,370 Sorry built in class in Python it's a built in class called S T R string built in string class chord 45 00:04:06,580 --> 00:04:17,770 s t o I've created on another function here called calculate task again a past in a self variable here. 46 00:04:18,580 --> 00:04:31,840 And this function I've also attached some error handling Python allows you to implement arrows add exceptions 47 00:04:31,900 --> 00:04:36,700 to be handled by a program who allows your programs to handle arrows. 48 00:04:36,730 --> 00:04:44,490 An exception in order to do that you need to use a try and the accept statement. 49 00:04:44,560 --> 00:04:53,350 So here is my try statement and this is what I specified and here is the statement for my except so 50 00:04:53,350 --> 00:04:59,030 this try and accept there to handle any errors in the application. 51 00:04:59,050 --> 00:05:01,260 So basically what this is doing here. 52 00:05:01,270 --> 00:05:08,780 This function here any data that is passed into that variable we get is going to be the. 53 00:05:08,800 --> 00:05:15,850 Are you going to be any input so it's taking the input is getting the input from the user and then passing 54 00:05:15,850 --> 00:05:20,560 it into this variable here and it'll catch any errors here. 55 00:05:20,560 --> 00:05:22,140 Using this try. 56 00:05:22,420 --> 00:05:31,690 So if the answer if the answer evaluates 2 equals 2 that a tool to display it does not. 57 00:05:31,700 --> 00:05:34,320 If there is an error the error will be captured. 58 00:05:34,760 --> 00:05:38,940 Okay so this here is used to display the text of the answer. 59 00:05:39,520 --> 00:05:46,510 This will this also self-taught answer we display and the task has been imported. 60 00:05:46,650 --> 00:05:54,950 And here it's a syntax that will capture any invalid syntax you enter or any error messages to be captured. 61 00:05:56,410 --> 00:05:59,650 I've added two other functions here. 62 00:05:59,650 --> 00:06:03,320 This one here called display text and passed it. 63 00:06:03,320 --> 00:06:06,700 The self and then the value that the user inputs. 64 00:06:06,770 --> 00:06:09,720 So again this is what it will do. 65 00:06:09,820 --> 00:06:19,030 You take in the input from the user and if you decide to delete the little entry these are the parameters 66 00:06:19,030 --> 00:06:20,040 it would take. 67 00:06:20,170 --> 00:06:22,440 And if you're inserting it. 68 00:06:22,630 --> 00:06:25,930 This is a code that will handle the insert. 69 00:06:25,930 --> 00:06:27,760 Again this is a function. 70 00:06:27,760 --> 00:06:32,030 This here this function is responsible for displaying the text. 71 00:06:32,080 --> 00:06:36,420 This function here is responsible for clearing the display. 72 00:06:36,460 --> 00:06:45,050 OK so any input you enter here you can clear it using this piece of code there. 73 00:06:45,520 --> 00:06:48,490 So here and the bottom here whether this here. 74 00:06:48,850 --> 00:06:58,980 So we sat in the calculator so equals to this TKD method which we specified earlier on the T.K. route. 75 00:06:59,230 --> 00:07:04,440 So we are here both with this title. 76 00:07:04,450 --> 00:07:07,590 We using that to set the title for the calculator. 77 00:07:07,990 --> 00:07:09,700 And this is what we'll call it. 78 00:07:09,700 --> 00:07:10,780 Okay. 79 00:07:11,200 --> 00:07:12,910 This T.K. here is the route. 80 00:07:12,930 --> 00:07:18,420 So we're setting up the route of the application will be the calculator and app. 81 00:07:18,490 --> 00:07:21,790 The app here is refers to the application name. 82 00:07:21,790 --> 00:07:23,430 This is the name of the application. 83 00:07:23,420 --> 00:07:31,460 It's called calculator this here you use that to make the window a fixed window so the window cannot 84 00:07:31,460 --> 00:07:33,190 be resized. 85 00:07:33,200 --> 00:07:39,440 OK so hands up setting the height to force so you cannot resize the window of the calculator. 86 00:07:39,440 --> 00:07:44,090 Because of this line of code here and this is the main loop of the calculator. 87 00:07:44,090 --> 00:07:45,350 This is the event loop. 88 00:07:45,350 --> 00:07:51,470 This is what keeps the calculator running on to you exit the calculator. 89 00:07:52,160 --> 00:07:54,550 So that should be it. 90 00:07:54,610 --> 00:07:58,010 I'm presuming a click save. 91 00:07:58,160 --> 00:08:01,570 So let's do a quick review of our code. 92 00:08:01,580 --> 00:08:05,030 Make sure the code is also formatted properly. 93 00:08:05,030 --> 00:08:12,270 If your code is not formatted properly and you try to run it it can cause syntax errors and the editor 94 00:08:12,280 --> 00:08:13,480 will complain. 95 00:08:13,490 --> 00:08:19,640 So make sure you've got it structured the way I've structured might have also added some comments here. 96 00:08:19,670 --> 00:08:25,160 So anything you see here in green here these are comments. 97 00:08:25,160 --> 00:08:28,470 When you run the application the comments are ignored. 98 00:08:29,310 --> 00:08:29,810 OK. 99 00:08:29,840 --> 00:08:31,150 So these are comments. 100 00:08:31,320 --> 00:08:33,360 And also this here is a comment. 101 00:08:33,360 --> 00:08:36,710 Anyway you see the hash symbol is also a comment. 102 00:08:36,720 --> 00:08:39,450 OK so these are comments. 103 00:08:39,470 --> 00:08:44,600 It just helps give you a description of what we've done here. 104 00:08:44,600 --> 00:08:49,910 So this tells you here that this is the main class for the calculator. 105 00:08:49,910 --> 00:08:54,350 It tells you Here we are the initialize in the frame here. 106 00:08:54,420 --> 00:08:58,390 We're creating all the buttons for the calculator here. 107 00:08:58,640 --> 00:09:03,490 The user input we store in them as an entry widget. 108 00:09:03,530 --> 00:09:06,760 Here is the button for value. 109 00:09:06,770 --> 00:09:13,880 Number seven in here we've got value. 110 00:09:13,880 --> 00:09:15,020 Number eight. 111 00:09:15,020 --> 00:09:16,450 So these are our comments. 112 00:09:16,450 --> 00:09:19,290 Anything it's easy a pound sign is a comment. 113 00:09:19,700 --> 00:09:24,740 And so on so I've commented out just put a comment before each line. 114 00:09:25,130 --> 00:09:26,330 So you know what they do. 115 00:09:26,980 --> 00:09:27,800 OK. 116 00:09:27,950 --> 00:09:31,600 So make sure your code is formatted this way. 117 00:09:31,820 --> 00:09:35,600 In here there is a format option to endangered code. 118 00:09:35,600 --> 00:09:37,930 See if you notice knowing that properly. 119 00:09:38,300 --> 00:09:43,970 Make sure you do that so that the editor doesn't complaints you notice within the functions you see 120 00:09:43,970 --> 00:09:49,160 where the function is created and then the other actions the logic inside the function. 121 00:09:49,160 --> 00:09:55,340 There's indentation you can see that even with a class you can see the class the indentation from the 122 00:09:55,340 --> 00:09:59,150 class and the methods. 123 00:09:59,150 --> 00:10:03,790 If I scroll down you can see the indentation also. 124 00:10:04,120 --> 00:10:04,600 Okay. 125 00:10:04,680 --> 00:10:07,610 Here eyes all indented or the logic. 126 00:10:07,670 --> 00:10:12,750 It's indented underneath the respective methods. 127 00:10:12,760 --> 00:10:13,480 OK. 128 00:10:13,640 --> 00:10:18,780 So make sure is horse all sorted and structured in this way. 129 00:10:18,800 --> 00:10:27,380 So once you've done that you just click and save and let's try and run the application so click Run 130 00:10:28,010 --> 00:10:32,910 and to run module hopefully. 131 00:10:32,940 --> 00:10:33,180 Yep. 132 00:10:33,450 --> 00:10:34,460 Okay that's good. 133 00:10:34,460 --> 00:10:36,230 So this is the application here. 134 00:10:37,190 --> 00:10:40,100 Let's see if we can do some calculation. 135 00:10:40,340 --> 00:10:46,350 So that's eight times eight 64. 136 00:10:46,570 --> 00:10:56,800 We'll clear that nine plus nine 18 will clear that five fifty six minus eight. 137 00:10:57,310 --> 00:11:01,270 We'll clear that so all the one of we know use. 138 00:11:01,280 --> 00:11:08,990 Yes to a division we do hate to divide by six. 139 00:11:08,990 --> 00:11:10,460 Okay. 140 00:11:10,460 --> 00:11:15,460 Clear that nine divide by three. 141 00:11:15,460 --> 00:11:16,280 Can I that. 142 00:11:16,370 --> 00:11:20,870 So we've got all the buttons and the functionality working properly. 143 00:11:20,870 --> 00:11:27,280 So this concludes the series on creating a calculator with Python. 144 00:11:27,290 --> 00:11:29,210 Thank you for watching and bye for now.