WEBVTT

0
00:00.260 --> 00:06.980
Now, in the last lesson, we saw a couple of different ways of working with Tkinter widgets.

1
00:06.980 --> 00:11.240
We've already seen how we can create Labels,

2
00:11.240 --> 00:21.320
so something like this, and also Buttons that can be clicked and do some sort of action.

3
00:21.320 --> 00:27.110
Then we've also seen Entries, which is basically some sort of text box.

4
00:27.110 --> 00:32.510
And now I want to show you some of the other widgets that you can work with.

5
00:32.510 --> 00:40.130
For example, the Text Entry box, which allows you to use multiple lines of text or something like

6
00:40.130 --> 00:47.390
the Spinbox, which is basically a sort of counter, which lets you go up and down, and then a Scale,

7
00:47.390 --> 00:53.720
which is a slider, basically, that you can move along its axis and change its value.

8
00:53.750 --> 01:02.390
Then there's a Checkbox which can be on or off, basically just a Tick box, Radio buttons, and finally

9
01:02.390 --> 01:05.390
a Listbox of choices which you can pick from.

10
01:06.560 --> 01:12.470
So these are pretty much all the widgets that you can work with. To save you from typing a lot of this

11
01:12.470 --> 01:19.770
example code, I've created a Tkinter widget demo for you, which you can go to, and you can see some

12
01:19.770 --> 01:25.380
of the existing code that we've already created, like how to create a window, or how to create a label,

13
01:25.380 --> 01:27.240
and how to create a button.

14
01:27.240 --> 01:31.950
But I want to show you some of the other features by just looking at the code.

15
01:31.950 --> 01:38.280
For example, with the entry, we can actually insert some text for the entries to start off with.

16
01:38.280 --> 01:44.550
So notice how here we can click on it to edit it, but it's really got some starting text in there.

17
01:44.550 --> 01:50.640
So for example, if you wanted to create an email box, maybe you would write the word email to give

18
01:50.640 --> 01:52.320
the user a bit of a hint.

19
01:52.350 --> 02:01.740
Now you've also got a text box, which is a large area where the user can edit, and this is simply

20
02:01.740 --> 02:03.600
created with the text widget.

21
02:03.600 --> 02:11.880
You can set the number of lines as height and the width as the width of the box, and you can set the

22
02:11.880 --> 02:17.310
text to be focused so that the cursor starts out in that text box.

23
02:17.310 --> 02:21.450
And then we can insert some piece of text to begin with.

24
02:21.450 --> 02:27.900
And we can also get hold of the text inside the text box using the get() method.

25
02:27.930 --> 02:34.080
Now notice there's something a little bit weird like the END here and the END here,

26
02:34.080 --> 02:41.460
this is just an index to allow Tkinter to figure out which particular item you're referring to, and

27
02:41.460 --> 02:42.780
you don't have to change it ever.

28
02:42.780 --> 02:45.030
This is just the code that you use.

29
02:45.030 --> 02:51.120
You can keep the code exactly as it is here, and just modify the text that you want to insert.

30
02:51.120 --> 02:59.290
And this "1.0" basically refers to getting hold of the text, starting from the first line at the character

31
02:59.290 --> 02:59.920
0.

32
03:00.850 --> 03:08.080
And then we've got a new widget that we've never really seen before, a Spinbox, which looks something

33
03:08.080 --> 03:09.940
like this.

34
03:09.940 --> 03:18.370
So you can click on the up and down to change its value, and we can print out the value that's being

35
03:18.370 --> 03:25.810
changed in there by simply getting the spinbox to be tied to a function, which is called spinbox_used()

36
03:25.810 --> 03:26.320
.

37
03:26.320 --> 03:28.810
And then we just get the value each time.

38
03:30.070 --> 03:32.950
Now, a Scale is also pretty self-explanatory,

39
03:32.950 --> 03:38.560
you can move it up and down, and we're getting hold of the value that where the user is landing on

40
03:38.560 --> 03:44.440
by simply packing again another command into that scale when we're creating it.

41
03:44.440 --> 03:49.960
And then when that function is called, it actually passes over the value that the scale is currently

42
03:49.960 --> 03:50.320
on,

43
03:50.320 --> 03:52.390
and that is what's getting printed.

44
03:54.160 --> 04:03.910
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

45
04:03.910 --> 04:06.400
checkbox to a variable.

46
04:06.400 --> 04:12.700
And this variable is something that's defined by the Tkinter module as an IntVar().

47
04:12.700 --> 04:14.710
So this is actually a class.

48
04:14.710 --> 04:21.280
And once we create that object from the class, then we can add it to our Checkbutton when we create

49
04:21.280 --> 04:21.640
it,

50
04:21.640 --> 04:26.110
and that variable will keep track of the value of that checkbox.

51
04:26.110 --> 04:29.140
One for on and zero for off.

52
04:29.620 --> 04:33.760
And then we can print it out when that checkbutton is used.

53
04:35.590 --> 04:39.760
Now Radio buttons are useful to pick between different options.

54
04:39.760 --> 04:45.310
So normally you'd have a number of options and only one of them can be selected at any one time.

55
04:45.310 --> 04:53.260
So in this case, what we do is again we create an IntVar(), and we tie that var to each of the radio buttons

56
04:53.260 --> 04:57.110
that we create, and then we give each of the radio buttons a value.

57
04:57.110 --> 04:58.370
So this one has value 2,

58
04:58.370 --> 04:59.750
this one has value 2,

59
04:59.750 --> 05:03.470
and they're both tied to that radio state int var.

60
05:03.470 --> 05:11.270
And then when that button gets changed, then we can actually get hold of the radio_state by calling

61
05:11.270 --> 05:12.500
radio_state.get(),

62
05:12.500 --> 05:16.430
and we get whichever value it is that's currently selected.

63
05:16.430 --> 05:18.590
So option1 or option2.

64
05:18.890 --> 05:26.630
Now finally we've got a Listbox which is just a list of options created from a Python list.

65
05:26.630 --> 05:34.850
We loop through each of the items, insert it to our listbox, and then once the listbox is used, we

66
05:34.850 --> 05:39.890
use this bind function to call this particular callback.

67
05:39.920 --> 05:47.390
This way, whenever we select any of the items in here, it will print out and get the current selection.

68
05:48.380 --> 05:53.330
Now, a lot of this code is pretty much going to be used the same each time.

69
05:53.330 --> 05:59.000
So it doesn't really matter if you don't understand fully exactly what some of these weird components

70
05:59.000 --> 05:59.510
are.

71
05:59.510 --> 06:09.200
It's just a factor of how this Tkinter library has taken this TK module and turned it into a Python

72
06:09.200 --> 06:12.470
format for us to be able to interact with it using Python.

73
06:12.470 --> 06:20.330
So there's a few weird bits like this ListboxSelect or this END being used here, but you can

74
06:20.330 --> 06:26.120
see that each line is commented and it tells you what it does, so you'll be able to figure out what

75
06:26.120 --> 06:34.260
line of code you need to write in order to add some text, or to get some text, or how to bind a function

76
06:34.260 --> 06:40.260
to the listbox, and how to get hold of the value that's currently being selected.

77
06:41.250 --> 06:48.090
I recommend heading over to this link here, and either downloading the code and opening it in PyCharm

78
06:48.090 --> 06:54.120
if you prefer, because then you can actually get a much larger version of the running program.

79
06:54.120 --> 06:59.820
Or alternatively, just have a play around with the code in here so that you understand how these widgets

80
06:59.820 --> 07:03.630
work and how you can use it basically as a palette.

81
07:03.630 --> 07:10.140
If you wanted a couple of checkboxes, or if you wanted a couple of spinboxes, then you can just pick

82
07:10.140 --> 07:16.380
and choose them from this cookbook, essentially, and then you can put them into your programs when

83
07:16.380 --> 07:17.550
and as needed.