The full Raspberry Pi Workshop in step-by-step format can be found here http://coreelec.io/piworkshop You can think of this section as a handy reference to copy code from and quickly deploy a widget.

Transcript

Okay let's take a look at a bunch of other TKinter widgets that we can use. So I have a simple GUI program running at the moment, over to the right, and this is this is our GUI so we have a couple of checkboxes, we've got some radio buttons, those are good for mutually exclusive options, and checkboxes are good for controlling things individually. I have this slider that can go between 0 and 100 and I have this spin box as well which is just a neat way to select between some predefined values, I've chosen one all the way up to 11, so let's have a bit of a play around, let's imagine that this one is connected to some lighting for instance so it could turn the lights on and off and we can see in the shell, the Python shell, that checkbox number one is now one to indicate that that's checked if we turn on the sprinklers, checkbox number two is now one, to indicate that that's checked as well, and of course if we turn them off then they both reset to zero. Underneath that I have this label widget so that's a neat way to include text in your interface, and in this case we can actually make the text dynamic, so we can use it to display a status for instance if I select option 1 I've got Radio Button One selected, that text has been updated and of course it makes sense but that should turn into a two once we select option 2. Playing with the slider we can set this to something like 24 and hit the load button and in the shell we have that value returned, so there's clearly some some variables being used now, there's variables that are storing the values for the checkboxes and for the radio buttons and indeed this slider has a much larger range, I haven't connected this spin box to anything, it's not too hard to imagine this number could be spat out in the terminal quite similarly. So let's have a look at how each of these widgets are built.

Over in the script itself, we've got a couple of imports again that's the same as last time, we don't have any GPIO because we're not driving any LEDs, we're making the window the title and the font and then we're using something called frames, I'll come back to that. These are the variables that we interacted with so when we started to check some boxes, select some options, these are the variables that were being updated and TKinter needs to use these objects called inter VARs or double VARs to hold the values for whatever comes out, so much like how we could attach say a command to a widget we can also attach variables to certain widgets as well, and we'll see that when we get to defining the widgets.

I'll skip the event functions so now that they're quite simple, let's just scroll down to where the widgets are defined so this this has everything that we see on the screen we've got the button that's the the load button, we have two check boxes a couple of radio buttons we've got that slider bar, the spin box, and of course be exit button so this this should all look now kind of similar because we just share a lot of the same properties declaring the widgets gets quite repetitive and that's why we're not doing a code along for this one we're just taking a quick look you can kind of think of this section as providing a neat little chunk of code that you can pull out and copy into your own script and that way you can very easily start to build these GUISs rather than having to type everything out manually that would just be excruciating. So let's have a look at the check boxes; we have check one is is an object that we're creating and we're creating a check button now rather than put it in the window like we did last time, I'm putting it in this thing called left frame and using frames is how we can organize our widgets perhaps a little more easily and quickly than using the grid when we use the grid to explicitly place widgets, using a frame I'll let a frame is a widget itself really a frame is just a widget that can contain other widgets and even other frames so if we create a frame and fill it with widgets, we can move that entire group of widgets around very easily we've got some text this is where we have attached the variable so check value one, the inter VAR check value one has been attached to check one, that makeS sense, and it's also connected to some command called check toggle. A similar thing is happening for check two we've got check value two and it's connected to the same command, that's okay, now let's return to framing for a minute. In this GUI, we can see if you look, if you look closely perhaps you can see that with arrange things into what's essentially three columns but they're really three frames so I have this left frame containing the check boxes this text label and the radio buttons I have a middle frame containing the slider and it's load button and then at some frame on the right containing the spin box and the exit button.

So when you look at the definitions for those objects you'll see the check button and their associated radio buttons and even the label wherever that is where is that Label, there it is. So the check buttons the radio buttons and the labels have all been put in the left frame, so that's been that's been done with a command called pack and pack does exactly what it says on the box it just allows tkinter to efficiently pack these objects as densely as possible so they form one group so we're packing everything into the left frame but what are we doing with this frame well right at the top here under my GUI definitions, I'm just creating an object called left frame and I'm calling and making it a frame object and that's where I'm putting it in win, which is the window, and then much like all the other objects I'm packing that frame on the left hand side. So just to reiterate you can make a frame, and pack it and you can fill that frame or you can pack that frame with other widgets and even other frames, so this is a really useful way for us to group items that have similar functionality or are all intended for some job, like if we had a home automation GUI we could have all the lighting in one frame, we could have some irrigation in another frame, and we could have let's say air conditioning in another frame and that would be perhaps a neat way to arrange it. Continuing on to let's take the slider this time we have the we have a slider variable where we're making it something called scale so this widget is actually called the scale in TKinter I just decided to call it a slider it's being packed into the mid frame with the pack command and it's been assigned to the mid frame of course we've got some variable that we're assigning to it and this time we have a couple of other arguments which are from underscore, fro underscores just being used because there's already a piece of syntax that exists called from, so to make it unique with used from under school and from and to a match of set the upper and lower limits somewhat counter-intuitively from is not the bottom of the scale from the top of the scale that's that's just a little idiosyncrasy. Similarly with the with the spin box we have we're creating an object called num option we're making it a spin box and we're putting it in the right frame again we've got a from and a too, and we can set some which so if we get really large numbers we can deal with that.

This time however I have decided to arrange the spin box in the top of the right frame so we can still kind of choose where objects sit within a frame, because I wanted that spin box to sit above the exit button, so we can see where we define the exit button I forced that to the bottom of the right frame so we have all of these variables being set whenever we interact with these widgets but what's actually happening behind the scenes, how are we pulling the value of these variables out. So all of these widgets has been attached to a command, except for the slider but we'll see why that isn't in a moment because it has that that load button, so if we go up to the event functions that have been programmed, let's take a look just first at the the button press. So that's that's this load button which has been attached to button press, and when we press that load button we get slider value is and then the slider value. So in that function we have a print statement and then we're just saying a regular print command with a format and what we're performing is sliderval.get so remember slider valve is something called a double var now that's not just a number it has a few other properties associated with it but if we want to extract the numeric value that that double var has within it, we use this this function .get and you'll see that for the checkbox values as well so we when we toggle the checkbox and we get this text appear in the check total function we have just our our text with some formatting and we have check Val one and checks out - don't get so this is why both the check boxes were pointing to the same function because we want to display the status as both checkboxes regardless of which one is clicked, we just want to indicate that something has been updated.

And of course the radio button when actually printing anything to the screen here, what were actually doing is modifying the text of this label so the radio buttons point to a command called check radio which is up here and what all check radio is doing is creating some string called select, so we're setting select is equal to and we concatenate the string radio button and we're concatenated that with rad.get which is going to return an integer, but we want a string, so all I'm doing then is tacking that within the STR for convert to string function so we're concatenated radio button some number selected. So we've got rad get returning an integer and then STR converting that integer into a string value for that to show that number as a string.

And that's about it so you can you can think of this script as essentially a template to base your other scripts off so you can very quickly pull functional pieces of code out of it

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.