[MUSIC] [SOUND] In this lecture we'll show a small example of how a human can interact with the Raspberry Pi through the library. So somebody can write a Python program that uses that library, and allows the user to have the right control over something hardware, some aspect of the hardware, through these widgets and library components that appear on the screen. So, here's our small example using a Scale Widget. So a Scale Widget is basically that slider that you see right there. It's called a Scale. And Tkinter. So our program to generate that. we start off with this from Tkinter import star, so we import. Master = tk, so that's creating the master window, right? Or the root window, whatever you want to call it. So we create that. Then we create the scale. So w = scale and we call this function scale and it generates the scale. And it returns w, which is the scale root, the object to represent that scale. And scale in this case takes several arguments. Some of them are required and some of them are not, so master is the first argument. The first argument is actually the name of the root window, which is mater in our case, so you've gotta pass through that argument. And then from _0 to 100, that gives you the range of the scale. So every scale has to have a range of numbers. See, these scales, they all have an integer inside or not necessarily an integer but in our case an integer. They have a value, a numerical value inside there associated with how far the scale has moved to the left or the right. So that value has to have a range from something to something. So that's what those two arguments are from 0 to 100. This scale starts at 0. If you moved it all the way to the right it would go to 100. And you have to give it the some kind of a range. Now then the next argument which is optional is the orientation. It says orient = horizontal. We've made a horizontal scale here. If you didn't give it that I think the default is actually vertical so it could do a vertical up down scale but either way, you can give it an orientation if you want to. So, that call to scale, it returns this scale object, which is assigned to w, so w is now a scale. And then to make that appear on the screen, we have to say w.pack, and then it appears on the screen, and you see something like depending on what you're using. You see something that looks like the picture on the screen that scale. Now this draws the Scale Widget and the user can now move it. The user can take their mouse and grab on the widget, and grab on the slider and slide it to the left and the right. Now that's part of our gooey. But that doesn't do anything particularly interesting yet. It draws a scale and you can move it left to right. But now we wanna interact with something like that, right? We wanna allow the user to use that scale to control something. Let's say have some pulse width modulated signal, okay? And we've talked about, in the previous lecture, how to make a pulse width modulated signal on a pin. Say we've got that, and we want the pulse width to change and to be controlled by the user through the scale. So, this, so our scale was from 0 to 100, right? So, let's say the duty cycle was that value. And, if the scale was moved all the way to the left, its at 0, then the duty cycle is 0, which means its low all the time. There's pulse, pwm signal. And, they move it all to the right its 100, so its high all the time. And, they can move it in between, 50, and its high half the time, low half the time, and so on. So, we want to make something with a pulse width, we have a pulse width modulated signal, the pulse width is controlled by that scale, so by the user, through this scale. So, how do we do this? So first thing, we'd have to change our original program a little bit. So remember that line of code where we said w = scale. We call that function scale, right? So now we're changing that call. The first four arguments are the same, right? Master, the root window, the from and to, the range 0 to 100. Orient horizontal, make a horizontal orientation. But we're adding this command = update. So what that is, that's whats called a reference tool call back function. Now, the call back function, in this case, is called, update. What that means is that any time a user actually moves that slider, it's called this update function. And what it, what it does is it passes an argument to that update function. It passes a single argument, the number inside that is associated with this scale. So this scale goes from 0 to 100, right? So that number 0 to 100, according to you know, whatever the scale is on, that is, passes an argument to the update function. Now this update function. You might ask well what is this update function? This update function is whatever you want it to be. So you now have to update this update function. So you can see we've written an definition of an update function. Def update duty. It takes an argument right? Remember I said this update function has to take one argument, the value from 0 to 100 that the scale is containing, right? It takes it as an argument and we just inside there we call pwm.ChangeDuty Cyclefloatduty. We just change the duty cycle, make duty into a float, and then we pass that to Change Duty Cycle. Whatever that scale value is, whatever the new scale value is because this update function it only gets called if somebody moved that scale. If they move that scale and they set the scale now at 25. Then a duty will equal 25 when you make that call and you'll change the duty cycle to 25. And then if they take that scale and move to 50 update will get called again, duty will be 50, you change the duty cycle to 50. So, in this way, by adding that command = update, we've added what's called a callback function, right? A reference to a callback function. Basically, a callback function is a generic term. It means a function that is called when an event happens, okay? And it's often used with graphic user interfaces. So, updates have callback function, we have to define it. And note, we don't have to call that function update. We chose to call it update. We could have used any name we wanted to. Okay? In this case we use update. So and we can define the update function and do whatever we want. In this case it's changing pulse with modulation. But you could be changing, I don't know, whatever you want. So yeah, the function takes the slider value of an argument and that's a way you can get interaction to work. Thank you. [MUSIC]