1
00:00:00,480 --> 00:00:01,859
Now in the last lesson,

2
00:00:01,920 --> 00:00:06,750
we saw a couple of different ways of working with tkinter widgets.

3
00:00:07,290 --> 00:00:11,190
We've already seen how we can create labels,

4
00:00:11,490 --> 00:00:16,490
so something like this, and also buttons that can be clicked and do some sort of

5
00:00:20,340 --> 00:00:23,730
action. Then we've also seen entries,

6
00:00:24,570 --> 00:00:26,940
which is basically some sort of textbox.

7
00:00:27,420 --> 00:00:32,369
And now I want to show you some of the other widgets that you can work with.

8
00:00:32,759 --> 00:00:35,220
For example, the text entry box

9
00:00:35,250 --> 00:00:40,250
which allows you to use multiple lines of text, or something like the spin box

10
00:00:41,340 --> 00:00:43,350
which is basically a sort of counter

11
00:00:43,680 --> 00:00:47,430
which lets you go up and down, and then a scale

12
00:00:47,430 --> 00:00:49,020
which is a slider

13
00:00:49,020 --> 00:00:53,460
basically that you can move along its axis and change its value.

14
00:00:54,090 --> 00:00:59,090
Then there's a check box which can be on or off basically just a tick box,

15
00:00:59,520 --> 00:01:04,260
radio buttons, and finally a list box of choices

16
00:01:04,260 --> 00:01:05,310
which you can pick from.

17
00:01:06,840 --> 00:01:11,220
So these are pretty much all the widgets that you can work with. To save you from

18
00:01:11,220 --> 00:01:13,050
typing a lot of this example

19
00:01:13,050 --> 00:01:17,730
code, I've created a tkinter widget demo Repl.it for you

20
00:01:18,030 --> 00:01:21,870
which you can go to and you can see some of the existing code that we've already

21
00:01:21,870 --> 00:01:22,703
created

22
00:01:22,770 --> 00:01:27,660
like how to create a window or how to create a label and how to create a button.

23
00:01:28,140 --> 00:01:32,370
But I want to show you some of the other features by just looking at the code.

24
00:01:32,850 --> 00:01:34,710
For example, with the entry

25
00:01:34,740 --> 00:01:38,730
we can actually insert some text for the entries to start off with.

26
00:01:39,060 --> 00:01:42,060
So notice how here we can click on it to edit it,

27
00:01:42,360 --> 00:01:46,350
but it's really got some starting text in there. So for example,

28
00:01:46,350 --> 00:01:48,690
if you wanted to create an email box,

29
00:01:48,720 --> 00:01:52,740
maybe you would write the word email to give the user a bit of a hint.

30
00:01:53,220 --> 00:01:56,280
Now you've also got a text box

31
00:01:56,310 --> 00:02:00,420
which is a large area where the user can edit.

32
00:02:01,650 --> 00:02:04,110
And this is simply created with the text widget.

33
00:02:04,410 --> 00:02:09,410
You can set the number of lines as height and the width as the width of the box.

34
00:02:11,250 --> 00:02:16,250
And you can set the text to be focused so that the cursor starts out in that

35
00:02:16,740 --> 00:02:17,580
textbox.

36
00:02:18,240 --> 00:02:23,240
And then we can insert some piece of text to begin with and we can also get hold

37
00:02:24,570 --> 00:02:29,460
of the text inside the text box using the get method. Now notice

38
00:02:29,460 --> 00:02:31,290
there's something a little bit weird

39
00:02:31,320 --> 00:02:34,560
like the end here and the end here.

40
00:02:34,890 --> 00:02:38,040
This is just an index to allow tkinter

41
00:02:38,040 --> 00:02:42,660
to figure out which particular item you're referring to and you don't have to

42
00:02:42,660 --> 00:02:45,420
change it ever. This is just the code that you use.

43
00:02:45,840 --> 00:02:50,460
You can keep the code exactly as it is here and just modify the test that you

44
00:02:50,460 --> 00:02:51,390
want to insert.

45
00:02:52,020 --> 00:02:57,020
And this 1.0 basically refers to getting hold of the text

46
00:02:57,090 --> 00:03:00,370
starting from the first line at the character zero.

47
00:03:01,750 --> 00:03:06,750
And then we've got a new widget that we've never really seen before, a spin box,

48
00:03:07,300 --> 00:03:10,480
which looks something like this.

49
00:03:10,720 --> 00:03:15,720
So you can click on the up and down to change its value and we can print out the

50
00:03:17,320 --> 00:03:22,320
value that's being changed in there by simply getting the spin box to be tied to

51
00:03:24,340 --> 00:03:26,800
a function which is called spinbox_used.

52
00:03:27,130 --> 00:03:29,170
And then we just get the value each time.

53
00:03:30,940 --> 00:03:33,580
Now a scale is also pretty self explanatory.

54
00:03:33,580 --> 00:03:37,750
You can move it up and down, and we're getting hold of the value that would

55
00:03:37,750 --> 00:03:40,810
the user is landing on by simply packing, again,

56
00:03:40,870 --> 00:03:44,890
another command into that scale when we're creating it.

57
00:03:45,220 --> 00:03:47,110
And then when that function is called,

58
00:03:47,110 --> 00:03:51,640
it actually passes over the value that the scale is currently on and that is

59
00:03:51,640 --> 00:03:55,480
what's getting printed. Next

60
00:03:55,510 --> 00:03:59,830
we've got a checkbox and we can click it on or off.

61
00:03:59,920 --> 00:04:04,920
And we can print one or zero by simply tying that checkbox to a variable.

62
00:04:07,240 --> 00:04:12,240
And this variable is something that's defined by the tkinter module as a IntVar.

63
00:04:13,360 --> 00:04:15,070
So this is actually a class.

64
00:04:15,550 --> 00:04:18,490
And once we create that object from the class,

65
00:04:18,550 --> 00:04:22,029
then we can add it to our check button when we create it,

66
00:04:22,480 --> 00:04:27,480
and that variable will keep track of the value of that checkbox. 1 for on and

67
00:04:28,510 --> 00:04:31,930
0 for off. And then we can print it out

68
00:04:31,990 --> 00:04:34,150
when that check button is used.

69
00:04:36,490 --> 00:04:40,300
Now radio buttons are useful to pick between different options.

70
00:04:40,540 --> 00:04:44,320
So normally you'd have a number of options and only one of them can be selected

71
00:04:44,380 --> 00:04:48,460
at any one time. So in this case what we do is, again,

72
00:04:48,460 --> 00:04:52,570
we create a IntVar and we tie that var to 

73
00:04:52,600 --> 00:04:54,760
each of the radio buttons that we create,

74
00:04:55,150 --> 00:04:58,990
and then we give each of the radio button a value. So this one has value 1,

75
00:04:58,990 --> 00:05:03,910
this one has value 2, and they're both tied to that radio state IntVar.

76
00:05:04,390 --> 00:05:07,570
And then when that button gets changed,

77
00:05:07,840 --> 00:05:12,550
then we can actually get hold of the radio state by calling radio_state.

78
00:05:12,580 --> 00:05:16,990
get and when you get whichever value it is that's currently selected,

79
00:05:17,020 --> 00:05:20,740
so option1 or option2. Now finally,

80
00:05:20,770 --> 00:05:22,720
we've got a list box

81
00:05:22,750 --> 00:05:27,250
which is just a list of options created from a Python list.

82
00:05:27,310 --> 00:05:29,020
We'd loop through each of the items,

83
00:05:29,350 --> 00:05:34,350
insert it to our listbox and then once the list box is used,

84
00:05:35,290 --> 00:05:40,290
we use this bind function to call this particular call back. This way

85
00:05:41,620 --> 00:05:44,350
whenever we select any of the items in here,

86
00:05:44,620 --> 00:05:49,510
it will print out and get the current selection. Now,

87
00:05:49,570 --> 00:05:53,890
a lot of this code is pretty much going to be used the same each time.

88
00:05:53,920 --> 00:05:58,220
So it doesn't really matter if you don't understand fully exactly what some of

89
00:05:58,220 --> 00:05:59,930
these weird components are.

90
00:06:00,230 --> 00:06:05,230
It's just a factor of how this tkinter library has taken this tk module and

91
00:06:07,940 --> 00:06:12,590
turned it into a Python format for us to be able to interact with it using

92
00:06:12,590 --> 00:06:13,100
Python.

93
00:06:13,100 --> 00:06:17,750
So there's a few weird bits like this listbox select or this,

94
00:06:17,840 --> 00:06:19,880
um, end being used here,

95
00:06:20,390 --> 00:06:24,230
but you can see that each line is commented and it tells you what it does.

96
00:06:24,560 --> 00:06:28,130
So you'll be able to figure out what line of code you need to write

97
00:06:28,130 --> 00:06:33,130
in order to add some text or to get some texts or how to bind a function to the

98
00:06:35,480 --> 00:06:40,480
list box and how to get hold of the value that's currently being selected.

99
00:06:41,960 --> 00:06:46,960
I recommend heading over to this link here and either downloading the code and

100
00:06:47,150 --> 00:06:49,490
opening it in PyCharm if you prefer,

101
00:06:49,850 --> 00:06:54,560
because then you can actually get a much larger version of the running program

102
00:06:54,860 --> 00:06:56,120
or alternatively,

103
00:06:56,150 --> 00:07:00,080
just have a play around with the code in here so that you understand how these

104
00:07:00,080 --> 00:07:04,010
widgets work and how you can use it basically as a pallet

105
00:07:04,370 --> 00:07:06,860
if you wanted a couple of checkboxes,

106
00:07:06,860 --> 00:07:09,800
or if you wanted a couple of a spin boxes,

107
00:07:09,800 --> 00:07:13,670
then you can just pick and choose them from this cookbook essentially

108
00:07:13,940 --> 00:07:17,990
and then you can put them into your programs when and as needed.

