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 text box.
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 Spinbox, 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 Checkbox which can be on or off, basically just a Tick box, Radio buttons, and finally
a Listbox 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 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 text box.
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 text 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
0.
And then we've got a new widget that we've never really seen before, a Spinbox, 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 spinbox 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 where 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 1 or 0 by simply tying that
checkbox to a variable.
And this variable is something that's defined by the Tkinter module as an IntVar().
So this is actually a class.
And once we create that object from the class, then we can add it to our Checkbutton when we create
it,
and that variable will keep track of the value of that checkbox.
One for on and zero for off.
And then we can print it out when that checkbutton 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 an IntVar(), and we tie that var to each of the radio buttons
that we create, and then we give each of the radio buttons a value.
So this one has value 2,
this one has value 2,
and they're both tied to that radio state int var.
And then when that button gets changed, then we can actually get hold of the radio_state by calling
radio_state.get(),
and we get whichever value it is that's currently selected.
So option1 or option2.
Now finally we've got a Listbox which is just a list of options created from a Python list.
We loop through each of the items, insert it to our listbox, and then once the listbox is used, we
use this bind function to call this particular callback.
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 ListboxSelect or this 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 text, or how to bind a function
to the listbox, 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 palette.
If you wanted a couple of checkboxes, or if you wanted a couple of spinboxes, 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.