Let's have a look at events on the Raspberry Pi now, events are not just when we're clicking on buttons on the interface but events can come from the user directly by the keyboard and mouse, for instance, a double click event or a certain key being pressed these events can all be bound to certain functions just like a widget can be bound to a function. So here's a list box and you can see that we can select various items but if we double click on an option, we get the behavior or the appearance of this window changing so we can we can double click on various items to display various options and for pretty neat-looking menu structure so let's have a look at how that's achieved.
Over in the script we've got the same old import the same old definitions we create some frames, I'll ignore the event functions for now and just go down to where we define this list box so that's something called lb, I'm creating a variable called lb and making it a list box object, and then we can just insert into the list box various options. So in the first place of course we have the irrigation controller and so forth but something else that's happening with lb is lb bind what we're doing here is binding a event so not only are we binding some event to some function that is we're binding a double button one which is a double left-click we're binding that event to menu manage but what we're also doing is allowing that binding to only occur while we're on lb the listbox so if I double click over here nothing happens but if I double click over here of course we get the options updating so a neat feature of TKinter is that we can we can set this up so that the binding only occurs while the mouse is over that widget.
Now let's have a look at menu manage to see what that's doing. This is the menu manage event, I just have to put a dummy variable in because it expects an argument but we're we're kind of hacking the functionality of this list box a little bit the first thing that we do is hide all the buttons and where we can pack a frame to make it appear what we can also do this pack forget and that makes it disappear so we're hiding all the buttons. What we're also doing is we're creating a variable called cell and we're giving it the value of the selection of the list box, so if I double click on dog walker that's the second item which is the which is the essentially one'th index so cell will be fetching the value one the integers value one, so every time we double click on the list box the variable cell is being updated with the the index of whichever item we double clicked on. We hide the frames and then we do a bit of an if-else chain we do this chain to show the relevant button so if we've clicked on the the first or 0th entry, then we're going to show the irrigation frame which is the on/off and if we have selected the seconds which is the one'th then cell will be returning a 1 in its integer. Now the reason I have to use this square bracket notation is because cell is not just some integers actually an object containing a bit of other information but the zeroth entry in cell is the is the index of whichever selection was being made. And that's that's essentially how you could could hack this list box into a cool little efficient menu structure so if we were to look at how these buttons are appearing and disappearing we've created the menu frame and that's always packed but then once created the irrigation and calculate a frame and we only pack and unpack those inside menu manage so that that changing of what items are visible only occurs during menu manage and of course the buttons that are associated with each of those they're packed into the relevant frames: the irrigation, dog and the calc frame, so we're able to hide and show two buttons at a time not by individually unpacking or pack forgetting those buttons but by packforgetting the frame of those buttons were packed into.
Now just for a quick demonstration let's have a look at this line of code here. We've got a commented out lb binds line so let's let's uncomment that you can use alt+4 to uncomment and let's comment the other bind that was already running with alt+3 so now we're bound double button one not to the function menu manage but to call back and if we run this script now you can see we have the same GUI as before but when we double-click we actually get these two numbers appear in the shell and what they are a coordinates of where the mouse is clicked so if I click right up next to the top left corner you can see that gets that gets toward 0 so 0 is in the top left but what this is just here to demonstrate is that events are very very flexible and very powerful and then the supplementary material for this section I've included a link to some documentation about other events and a few examples of how that can be used.
So this event that is being triggered now call back let's have a look at that function. Call Back is being triggered by double button 1 so that is that double button one is the event that's being passed to call back which means that as that is essentially all the properties of that double button click for that double double click on the one button sorry so what we can do is that we can access various properties is that the x and y coordinates and they're just being printed straight to the shell so you see that's a really really easy way to extract some mouse coordinates with double button click and that wraps up chapter 4. By now we can program simple graphical user interfaces, which is neat if you want to give your Raspberry Pi project a nice polished finish. I'll see you in chapter 5.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.