1 00:00:00,031 --> 00:00:03,359 Hi again. And in the previous lecture, we 2 00:00:03,359 --> 00:00:06,299 build this graphical user interface with 3 00:00:06,299 --> 00:00:09,689 tkinter. And the problem is, this does 4 00:00:09,719 --> 00:00:11,669 nothing for now. So we press these, 5 00:00:11,669 --> 00:00:13,319 but it doesn't do anything. 6 00:00:13,559 --> 00:00:15,959 So what I'd like to do here that, the 7 00:00:15,959 --> 00:00:19,079 final output I'd like to have is when I 8 00:00:19,139 --> 00:00:21,329 execute this button, so we have 9 00:00:21,329 --> 00:00:23,189 an entry here, we can input the value, 10 00:00:23,429 --> 00:00:25,919 and I want to execute this button and 11 00:00:25,919 --> 00:00:29,909 get miles here in the text area. So 12 00:00:29,939 --> 00:00:33,029 miles is like one kilometre unit is 13 00:00:33,059 --> 00:00:37,619 equals to one mile times 1.6. So if I 14 00:00:37,619 --> 00:00:40,199 execute this, I want something like 16 15 00:00:40,199 --> 00:00:43,469 here to be generated. So a very simple 16 00:00:43,499 --> 00:00:46,019 program. Note that this is not one of 17 00:00:46,019 --> 00:00:47,969 the real programs that I have promised 18 00:00:47,969 --> 00:00:50,099 to build in this course. So this is just 19 00:00:50,099 --> 00:00:52,499 to get you a wrap around with tkinter. 20 00:00:53,129 --> 00:00:57,209 So let's close this now, and to have a 21 00:00:57,209 --> 00:00:59,699 button do something, we need to add 22 00:00:59,999 --> 00:01:03,689 command parameter here. So, a command 23 00:01:03,689 --> 00:01:06,359 parameter takes a function here as an 24 00:01:06,359 --> 00:01:08,909 argument, as a value. And then when you 25 00:01:08,909 --> 00:01:11,159 press that button, the Execute 26 00:01:11,159 --> 00:01:13,169 button in this case, the function that 27 00:01:13,169 --> 00:01:15,599 you have here will be executed. So let's 28 00:01:15,599 --> 00:01:18,330 say we have a function here, def 29 00:01:19,783 --> 00:01:22,689 km_to_miles. 30 00:01:22,714 --> 00:01:26,765 [No audio] 31 00:01:26,790 --> 00:01:30,610 And let's say this function for now, we just print out some 32 00:01:30,689 --> 00:01:34,589 text. Let's say Success, for instance. 33 00:01:35,969 --> 00:01:38,729 Because you know, it's good to before 34 00:01:38,729 --> 00:01:41,729 you write your actual program, it's good 35 00:01:41,729 --> 00:01:44,249 to use print statements to try things 36 00:01:44,249 --> 00:01:47,879 out. So that allows you to isolate your 37 00:01:47,879 --> 00:01:50,699 problems. If something happens here, you 38 00:01:50,699 --> 00:01:52,679 know that you can have a problem with a 39 00:01:52,679 --> 00:01:55,109 print statement, but if you had more 40 00:01:55,109 --> 00:01:57,749 complicated expression here, it gets 41 00:01:57,749 --> 00:02:00,989 harder to know whether a problem happens 42 00:02:01,019 --> 00:02:04,079 in here that you are not pointing to the 43 00:02:04,109 --> 00:02:06,929 correct function, or you have a problem 44 00:02:06,929 --> 00:02:09,059 with your statements. So it's good to 45 00:02:09,089 --> 00:02:11,219 use simple statements first, like the 46 00:02:11,219 --> 00:02:13,889 print statement. So you'd go here and 47 00:02:14,849 --> 00:02:17,129 km_to_miles, that is the function. 48 00:02:17,999 --> 00:02:20,669 And one thing you should keep in mind is 49 00:02:20,669 --> 00:02:23,069 that you don't pass in brackets here. So 50 00:02:23,069 --> 00:02:26,009 this is not a usual function, cool, 51 00:02:26,069 --> 00:02:28,169 you're just referencing to the function 52 00:02:28,169 --> 00:02:31,349 here, and I left Python taking care of 53 00:02:31,469 --> 00:02:33,749 all the rest. So the function name goes 54 00:02:33,749 --> 00:02:35,257 here, and let's try this old. 55 00:02:35,282 --> 00:02:38,702 [Author typing] 56 00:02:38,733 --> 00:02:39,741 Execute. 57 00:02:40,439 --> 00:02:43,049 And you see that we got Success printed 58 00:02:43,049 --> 00:02:45,329 out in here, press again, Success, 59 00:02:45,329 --> 00:02:47,459 Success, Success, so it's working. 60 00:02:47,729 --> 00:02:49,799 However, we don't need to print out 61 00:02:49,799 --> 00:02:52,829 Success in the command line, we want to 62 00:02:52,829 --> 00:02:54,881 print out a value in the text area. 63 00:02:54,906 --> 00:02:57,309 [Author typing] 64 00:02:57,334 --> 00:02:59,789 Now we need to look at the entry widget, 65 00:02:59,999 --> 00:03:02,549 because series where we are going to get 66 00:03:02,549 --> 00:03:04,799 the value from the user enters a 67 00:03:04,799 --> 00:03:07,349 value of the entry widget, and then we 68 00:03:07,349 --> 00:03:09,509 grab that value, and then we use that 69 00:03:09,509 --> 00:03:12,059 value in our function. And for this 70 00:03:12,089 --> 00:03:16,098 there is a parameter called textvariable. 71 00:03:17,700 --> 00:03:21,768 And that will be equal to a StringVar 72 00:03:21,793 --> 00:03:23,819 object, which we have to declare 73 00:03:23,879 --> 00:03:27,959 further up here. So let's say you want 74 00:03:27,959 --> 00:03:30,479 value, so that's the variable, and that 75 00:03:30,479 --> 00:03:33,239 would be equal to the StringVar 76 00:03:33,239 --> 00:03:37,525 function. And then we point to e1_value 77 00:03:38,268 --> 00:03:39,779 a variable, which we have just 78 00:03:39,779 --> 00:03:42,029 created. So this variable will get the 79 00:03:42,029 --> 00:03:44,729 value, depending on what the user will 80 00:03:44,729 --> 00:03:47,669 input in the entry widget, then we can 81 00:03:47,669 --> 00:03:51,269 use this value whatever we like. Let's 82 00:03:51,269 --> 00:03:55,829 say we want that print out. e1_value, and 83 00:03:55,829 --> 00:03:58,109 we also need to add something 84 00:03:58,109 --> 00:04:01,469 extra here, which is a get method of the 85 00:04:01,469 --> 00:04:04,979 StringVar object. So this is a 86 00:04:04,979 --> 00:04:07,559 special object, it's not a string, a Python 87 00:04:07,559 --> 00:04:10,379 plain string. To get an actual string 88 00:04:10,409 --> 00:04:12,929 out of this object, we need to 89 00:04:12,929 --> 00:04:16,169 apply the get method. So save this and 90 00:04:16,199 --> 00:04:19,019 try the script. Let's say 10 here, 91 00:04:19,199 --> 00:04:22,649 Execute, and we get 10 printed off on 92 00:04:22,649 --> 00:04:25,049 the terminal. So we haven't yet 93 00:04:25,079 --> 00:04:28,199 connected this string here with a text 94 00:04:28,199 --> 00:04:33,179 widget. So let's do just that. Here. The 95 00:04:33,179 --> 00:04:36,239 idea here is that we need to insert a 96 00:04:36,239 --> 00:04:39,299 value to the text widget. And to do 97 00:04:39,299 --> 00:04:41,759 that, the text widget has a method 98 00:04:41,759 --> 00:04:44,219 called insert. So you need to refer to 99 00:04:44,219 --> 00:04:47,309 the text widget that you want. So t1 100 00:04:47,309 --> 00:04:49,949 is our text object and then apply the 101 00:04:49,949 --> 00:04:52,649 insert method and the method gets an 102 00:04:52,679 --> 00:04:55,949 argument first, which is the place where 103 00:04:55,949 --> 00:04:58,739 you want to insert the text. Let's say 104 00:04:58,739 --> 00:05:01,049 we want to insert at the end of text 105 00:05:01,049 --> 00:05:04,379 widget. For example, let's suppose you 106 00:05:04,379 --> 00:05:06,839 are inserting many lines of text in your 107 00:05:06,869 --> 00:05:10,709 widget, and you'd have to apply to 108 00:05:10,709 --> 00:05:13,589 execute this method multiple times. And 109 00:05:13,589 --> 00:05:16,649 each time if you put END, you'd enter 110 00:05:16,649 --> 00:05:18,899 the new text at the bottom of the 111 00:05:18,899 --> 00:05:21,659 existing text. So you're saying put, at 112 00:05:21,684 --> 00:05:26,315 the end, this text. e1_value.get. 113 00:05:27,799 --> 00:05:31,409 So the string of the value that the 114 00:05:31,409 --> 00:05:34,199 user will enter in the entry widget, so 115 00:05:34,199 --> 00:05:37,979 let's see how these goes. Execute the 116 00:05:37,979 --> 00:05:40,829 function. And put some text here, you 117 00:05:40,829 --> 00:05:45,779 can also put anything, Hey there, Execute, 118 00:05:45,809 --> 00:05:48,359 and you get the string printed out in a 119 00:05:48,359 --> 00:05:51,989 text widget. All right, but how if we 120 00:05:51,989 --> 00:05:55,139 don't want to just grab this value and 121 00:05:55,199 --> 00:05:57,419 insert it in the text, we want to first 122 00:05:57,419 --> 00:05:59,729 do something with this value and then we 123 00:05:59,759 --> 00:06:02,699 insert the product, the output in the 124 00:06:02,729 --> 00:06:05,879 text widget. So let's say we will grab 125 00:06:05,879 --> 00:06:10,529 the e1_value.get, and we will 126 00:06:10,529 --> 00:06:15,869 multiply that by 1.6. That would be 127 00:06:15,899 --> 00:06:21,089 miles. So miles equals to the value that 128 00:06:21,089 --> 00:06:24,929 the user inputs, times 1.6. So 129 00:06:24,929 --> 00:06:26,729 you are asking the user to input 130 00:06:26,729 --> 00:06:29,969 kilometers, and one mile is equal to one 131 00:06:29,969 --> 00:06:33,539 kilometer times 1.6, actually it's 132 00:06:33,659 --> 00:06:36,719 something like 1.6 or 6, I don't 133 00:06:36,719 --> 00:06:38,819 remember the exact value, but you get 134 00:06:38,819 --> 00:06:41,339 the idea. And lastly, you'd want to pass 135 00:06:41,339 --> 00:06:43,769 miles in here. So you would want to 136 00:06:43,799 --> 00:06:46,229 insert the miles output value in the 137 00:06:46,349 --> 00:06:51,299 widget, save and execute. And I expect 138 00:06:51,299 --> 00:06:55,499 to get an error here, let's say 10. Yes, 139 00:06:55,499 --> 00:06:58,889 we got an error. So when you get errors, 140 00:06:58,889 --> 00:07:01,739 don't just try to copy this error and 141 00:07:01,739 --> 00:07:04,229 paste it on Google, and try to see what 142 00:07:04,259 --> 00:07:07,049 other people are saying, you may waste a 143 00:07:07,049 --> 00:07:09,839 lot of time. Instead, invest some time 144 00:07:09,839 --> 00:07:11,609 and try to understand this error. So 145 00:07:11,609 --> 00:07:13,589 what this is saying, it's a TypeError, 146 00:07:14,009 --> 00:07:17,219 can't multiply sequence by non-integer 147 00:07:17,519 --> 00:07:20,759 of type float. So a sequence can be a 148 00:07:20,759 --> 00:07:24,209 list, can be a string, a tupple, etc. And 149 00:07:24,209 --> 00:07:26,309 Python is trying to multiply a sequence 150 00:07:26,339 --> 00:07:28,799 with a non-integer, which is a float. 151 00:07:28,949 --> 00:07:32,789 So float here is the 1.6 number. And 152 00:07:32,789 --> 00:07:36,149 Python is trying to multiply 1.6 with a 153 00:07:36,149 --> 00:07:41,429 sequence, with this here. So this is not 154 00:07:41,489 --> 00:07:43,799 a number because the get method actually 155 00:07:43,829 --> 00:07:46,709 grabs a string out of a StringVar 156 00:07:47,009 --> 00:07:49,379 object, and a string cannot be 157 00:07:49,379 --> 00:07:52,199 multiplied with a float number. If 158 00:07:52,199 --> 00:07:54,329 this was to a, it was possible to 159 00:07:54,329 --> 00:07:57,419 multiply. For example, if you multiply 160 00:07:57,869 --> 00:08:01,679 The string me times two, you'd get me, me. 161 00:08:02,639 --> 00:08:07,379 So, but you cannot multiply me by 1.6 162 00:08:07,379 --> 00:08:08,999 because it doesn't make any sense. 163 00:08:09,629 --> 00:08:12,569 Anyway, so the solution to this is, you 164 00:08:12,569 --> 00:08:15,329 want to convert the value that the user 165 00:08:15,329 --> 00:08:18,449 inputs to a float. And you do that by 166 00:08:18,449 --> 00:08:23,279 adding the float function. So what's all 167 00:08:23,279 --> 00:08:26,399 for the brackets here? You see there is 168 00:08:26,399 --> 00:08:29,009 an underline on the bracket and you have 169 00:08:29,009 --> 00:08:32,579 an underline here. So this means this 170 00:08:32,579 --> 00:08:37,199 bracket closes in here. So everything 171 00:08:37,199 --> 00:08:41,819 looks fine. Save and execute again. 10 172 00:08:42,299 --> 00:08:45,059 and Execute, and we get the expected 173 00:08:45,089 --> 00:08:47,669 output, you can delete this print 174 00:08:47,669 --> 00:08:50,759 function, we don't need it anymore. And 175 00:08:50,759 --> 00:08:53,279 I believe that was a good start with the 176 00:08:53,279 --> 00:08:55,409 tkinter library, and you should now be 177 00:08:55,409 --> 00:08:58,079 able to make more advance interfaces. 178 00:08:58,739 --> 00:09:01,439 However, we will be building a real 179 00:09:01,439 --> 00:09:03,749 advanced graphical user interface with 180 00:09:03,749 --> 00:09:06,089 tkinter which will interact with a 181 00:09:06,089 --> 00:09:08,939 database. So it will be a real world 182 00:09:08,939 --> 00:09:11,639 program. And for now, I'll just hand you 183 00:09:11,639 --> 00:09:14,219 an exercise in the next lecture, so that 184 00:09:14,219 --> 00:09:16,709 you practice tkinter a little bit, and you 185 00:09:16,709 --> 00:09:19,949 also get to know with some more cosmetic 186 00:09:19,979 --> 00:09:22,439 aspects. So you'll make the problem 187 00:09:22,439 --> 00:09:24,479 that we built in this lecture, you will 188 00:09:24,479 --> 00:09:27,599 make it more visually appealing, and you 189 00:09:27,599 --> 00:09:29,279 will make sure you will get your hands 190 00:09:29,309 --> 00:09:31,799 dirty with tkinter a little bit. I'll 191 00:09:31,799 --> 00:09:32,939 talk to you later.