Now in the last lesson,
we saw a couple of different ways of working with tkinter widgets.
We've already seen how we can create labels,
so something like this, and also buttons that can be clicked and do some sort of
action. Then we've also seen entries,
which is basically some sort of textbox.
And now I want to show you some of the other widgets that you can work with.
For example, the text entry box
which allows you to use multiple lines of text, or something like the spin box
which is basically a sort of counter
which lets you go up and down, and then a scale
which is a slider
basically that you can move along its axis and change its value.
Then there's a check box which can be on or off basically just a tick box,
radio buttons, and finally a list box of choices
which you can pick from.
So these are pretty much all the widgets that you can work with. To save you from
typing a lot of this example
code, I've created a tkinter widget demo Repl.it for you
which you can go to and you can see some of the existing code that we've already
created
like how to create a window or how to create a label and how to create a button.
But I want to show you some of the other features by just looking at the code.
For example, with the entry
we can actually insert some text for the entries to start off with.
So notice how here we can click on it to edit it,
but it's really got some starting text in there. So for example,
if you wanted to create an email box,
maybe you would write the word email to give the user a bit of a hint.
Now you've also got a text box
which is a large area where the user can edit.
And this is simply created with the text widget.
You can set the number of lines as height and the width as the width of the box.
And you can set the text to be focused so that the cursor starts out in that
textbox.
And then we can insert some piece of text to begin with and we can also get hold
of the text inside the text box using the get method. Now notice
there's something a little bit weird
like the end here and the end here.
This is just an index to allow tkinter
to figure out which particular item you're referring to and you don't have to
change it ever. This is just the code that you use.
You can keep the code exactly as it is here and just modify the test that you
want to insert.
And this 1.0 basically refers to getting hold of the text
starting from the first line at the character zero.
And then we've got a new widget that we've never really seen before, a spin box,
which looks something like this.
So you can click on the up and down to change its value and we can print out the
value that's being changed in there by simply getting the spin box to be tied to
a function which is called spinbox_used.
And then we just get the value each time.
Now a scale is also pretty self explanatory.
You can move it up and down, and we're getting hold of the value that would
the user is landing on by simply packing, again,
another command into that scale when we're creating it.
And then when that function is called,
it actually passes over the value that the scale is currently on and that is
what's getting printed. Next
we've got a checkbox and we can click it on or off.
And we can print one or zero by simply tying that checkbox to a variable.
And this variable is something that's defined by the tkinter module as a IntVar.
So this is actually a class.
And once we create that object from the class,
then we can add it to our check button when we create it,
and that variable will keep track of the value of that checkbox. 1 for on and
0 for off. And then we can print it out
when that check button is used.
Now radio buttons are useful to pick between different options.
So normally you'd have a number of options and only one of them can be selected
at any one time. So in this case what we do is, again,
we create a IntVar and we tie that var to
each of the radio buttons that we create,
and then we give each of the radio button a value. So this one has value 1,
this one has value 2, and they're both tied to that radio state IntVar.
And then when that button gets changed,
then we can actually get hold of the radio state by calling radio_state.
get and when you get whichever value it is that's currently selected,
so option1 or option2. Now finally,
we've got a list box
which is just a list of options created from a Python list.
We'd loop through each of the items,
insert it to our listbox and then once the list box is used,
we use this bind function to call this particular call back. This way
whenever we select any of the items in here,
it will print out and get the current selection. Now,
a lot of this code is pretty much going to be used the same each time.
So it doesn't really matter if you don't understand fully exactly what some of
these weird components are.
It's just a factor of how this tkinter library has taken this tk module and
turned it into a Python format for us to be able to interact with it using
Python.
So there's a few weird bits like this listbox select or this,
um, end being used here,
but you can see that each line is commented and it tells you what it does.
So you'll be able to figure out what line of code you need to write
in order to add some text or to get some texts or how to bind a function to the
list box and how to get hold of the value that's currently being selected.
I recommend heading over to this link here and either downloading the code and
opening it in PyCharm if you prefer,
because then you can actually get a much larger version of the running program
or alternatively,
just have a play around with the code in here so that you understand how these
widgets work and how you can use it basically as a pallet
if you wanted a couple of checkboxes,
or if you wanted a couple of a spin boxes,
then you can just pick and choose them from this cookbook essentially
and then you can put them into your programs when and as needed.