1
00:00:00,420 --> 00:00:05,420
Let's get started by, as usual, importing the relevant library. Tkinter, like the

2
00:00:06,750 --> 00:00:11,250
turtle module, is already pre-installed with every installation of Python.

3
00:00:11,760 --> 00:00:13,380
As long as you've got Python running,

4
00:00:13,410 --> 00:00:17,760
then you should be able to simply import Tkinter as the module

5
00:00:18,000 --> 00:00:22,410
and then you'll be able to use all of the classes and properties and methods

6
00:00:22,440 --> 00:00:24,870
inside. So for example,

7
00:00:24,870 --> 00:00:27,720
we could start off by creating a new window,

8
00:00:28,380 --> 00:00:31,800
and this is sort of equivalent to the screens that we've been working with

9
00:00:32,040 --> 00:00:34,710
in turtle . So in order to do this,

10
00:00:34,740 --> 00:00:39,740
we have to dig into the Tkinter module and then find a class called Tk.

11
00:00:40,260 --> 00:00:43,620
And then once we initialize a new object from that class,

12
00:00:43,860 --> 00:00:48,300
then we have ourselves a window. But if I run this as it is,

13
00:00:48,450 --> 00:00:51,330
you'll see absolutely nothing happen

14
00:00:53,070 --> 00:00:56,550
and it will just go straight away to exit code zero.

15
00:00:57,150 --> 00:01:02,070
And the reason for this is because we need a way of keeping the window on the

16
00:01:02,070 --> 00:01:06,300
screen. What Tkinter has done here is it's gone ahead,

17
00:01:06,390 --> 00:01:09,570
created the window, sees that there's no more instructions

18
00:01:09,660 --> 00:01:12,360
and then it basically ended the program. Now,

19
00:01:12,360 --> 00:01:16,890
what we want instead is to sort of have a while loop that basically just keeps

20
00:01:16,890 --> 00:01:21,270
on running, so something like a while true. And inside this while loop,

21
00:01:21,390 --> 00:01:26,390
it holds onto this window and keeps on listening to see if there's anything else

22
00:01:26,790 --> 00:01:30,900
that the user is going to do to interact with that window so that it can

23
00:01:30,900 --> 00:01:32,880
respond when that happens.

24
00:01:33,300 --> 00:01:37,530
So this loop is included inside Tkinter

25
00:01:37,740 --> 00:01:42,740
and all you have to do is get your window to simply call the main loop.

26
00:01:44,190 --> 00:01:48,780
This main loop is the thing that will keep the window on screen like this.

27
00:01:49,200 --> 00:01:54,090
So now here is our window and here's the line of code that keeps it on the screen and

28
00:01:54,090 --> 00:01:57,990
listens for what the user will do to interact with it. For example,

29
00:01:57,990 --> 00:02:00,090
click on a button or type something.

30
00:02:00,810 --> 00:02:05,810
Now this line of code always has to be at the very end of your program and all

31
00:02:06,360 --> 00:02:07,860
the rest of the things that you're going to write

32
00:02:07,920 --> 00:02:11,850
go in between creating the window and getting the window to run.

33
00:02:13,020 --> 00:02:17,730
The next thing we can do is we can change the title of that program.

34
00:02:18,060 --> 00:02:20,160
And we've done this before using turtle.

35
00:02:20,460 --> 00:02:24,540
We just call the title method and we give a string as the input.

36
00:02:24,840 --> 00:02:27,360
So 'My first GUI program',

37
00:02:27,810 --> 00:02:30,450
and now if I rerun my code,

38
00:02:30,840 --> 00:02:35,610
then you can see that this is what's showing up at the very top bar of my

39
00:02:35,610 --> 00:02:37,170
program. Now,

40
00:02:37,200 --> 00:02:41,370
other things that we might want to change with the window is the sizing of it.

41
00:02:41,460 --> 00:02:46,050
So there is a default size and there's also a way of changing the size.

42
00:02:46,920 --> 00:02:50,930
And we can do that by tapping into a method called a minimum size.

43
00:02:51,450 --> 00:02:55,560
The window is going to scale to include all of the components that you put

44
00:02:55,560 --> 00:02:57,960
inside. If you put a hundred buttons inside,

45
00:02:57,960 --> 00:02:59,590
it's going to make the window massive.

46
00:02:59,950 --> 00:03:02,050
But if you don't have a lot of things in the window,

47
00:03:02,380 --> 00:03:07,380
then you might want to define a minimum size so that you can specify that, by

48
00:03:07,690 --> 00:03:11,290
default, I want my width to be, let's say, I don't know,

49
00:03:11,320 --> 00:03:14,920
500 and the height to be 300,

50
00:03:15,250 --> 00:03:17,740
unless the user wants to resize the window,

51
00:03:18,160 --> 00:03:21,790
or there's a lot of components that makes the window a lot bigger.

52
00:03:22,450 --> 00:03:23,530
So when we run our code

53
00:03:23,530 --> 00:03:28,530
now you can see this is a 500-pixel by 300-pixel window that we've just created

54
00:03:29,590 --> 00:03:31,240
with our own custom title.

55
00:03:32,980 --> 00:03:35,260
In addition to creating windows,

56
00:03:35,320 --> 00:03:39,760
we can, of course, create some components to put inside the window.

57
00:03:40,150 --> 00:03:41,410
So I want to show you firstly

58
00:03:41,410 --> 00:03:46,410
how you would create a label. Just as our window was created from the tkinker

59
00:03:46,990 --> 00:03:50,740
module and it's the Tk class that was used,

60
00:03:51,070 --> 00:03:54,340
there's also a class inside Tkinter called

61
00:03:54,430 --> 00:03:58,210
a label. So that we initialize a label class,

62
00:03:58,240 --> 00:04:00,370
and we can save that to a variable called

63
00:04:00,430 --> 00:04:03,670
my_label or anything else for that matter.

64
00:04:04,180 --> 00:04:08,050
Now this label has quite a few things that we can change about it.

65
00:04:08,590 --> 00:04:13,590
It might not be obvious from the documentation or from the prompt that PyCharm

66
00:04:15,220 --> 00:04:16,053
gives us,

67
00:04:16,120 --> 00:04:21,120
but we can, in fact, change a property called text so we can change what is

68
00:04:21,339 --> 00:04:26,290
displayed inside the label. Let's just write some text called 'I am a label'

69
00:04:27,310 --> 00:04:31,150
Unfortunately, that's actually not enough to get the label to display.

70
00:04:31,210 --> 00:04:35,830
So when I run my program as it is, you don't actually see a label anywhere

71
00:04:35,830 --> 00:04:38,830
on the screen. When we're working with Tkinter

72
00:04:38,830 --> 00:04:43,830
what we have to do is we first have to create a component like a

73
00:04:44,020 --> 00:04:44,853
label.

74
00:04:45,010 --> 00:04:49,750
And then we have to specify how that component is going to be laid out on the

75
00:04:49,750 --> 00:04:51,940
screen before it will show up.

76
00:04:52,330 --> 00:04:57,190
And one of the easiest ways of laying out any component is just to get hold of

77
00:04:57,190 --> 00:05:00,640
that component and then call the method called pack.

78
00:05:01,150 --> 00:05:05,800
And this is going to place it into the screen and it's going to automatically

79
00:05:05,800 --> 00:05:08,530
center it on the screen. So now

80
00:05:08,560 --> 00:05:11,170
if I run the code with this modification,

81
00:05:11,410 --> 00:05:16,410
you can see our label is now finally showing up and it's packed so that it's in

82
00:05:16,750 --> 00:05:18,520
the center of the program.

83
00:05:20,020 --> 00:05:23,740
Now there's other properties that we can modify about our label.

84
00:05:24,010 --> 00:05:27,430
So we could initialize our label with a piece of text,

85
00:05:27,670 --> 00:05:31,810
but we can also specify a font. The font is again,

86
00:05:31,810 --> 00:05:33,010
going to be a tuple,

87
00:05:33,430 --> 00:05:36,670
and that tuple is first going to take the name of the font,

88
00:05:36,700 --> 00:05:38,890
so let's say I want to use Arial,

89
00:05:39,340 --> 00:05:42,310
and then it's going to take the size of the font.

90
00:05:42,340 --> 00:05:47,290
So let's make it a little bit bigger, maybe a 24 size font. And finally,

91
00:05:47,290 --> 00:05:51,820
it's going to take whether, if it's bold or italic or, um,

92
00:05:51,850 --> 00:05:56,110
let's just set it to bold. And now if I run my code again,

93
00:05:56,470 --> 00:06:00,350
you can see I've now got a Arial font, size 24

94
00:06:00,620 --> 00:06:01,970
and it's bold.

95
00:06:02,780 --> 00:06:07,780
You can also change this to italic or you can simply just ignore it and leave

96
00:06:09,110 --> 00:06:12,590
out that last item and it will just give you a regular piece of text.

97
00:06:13,130 --> 00:06:18,130
But none of this would show up unless you have this line of code where you

98
00:06:18,260 --> 00:06:23,090
actually use the packer to pack our label onto the screen. Now

99
00:06:23,090 --> 00:06:24,290
in the course resources,

100
00:06:24,290 --> 00:06:29,290
I've linked to the Tkinter documentation on the Python docs and they talk

101
00:06:29,990 --> 00:06:32,720
about the packer in a lot more detail.

102
00:06:33,200 --> 00:06:36,020
It's basically a geometry management system.

103
00:06:36,020 --> 00:06:40,130
It's just a simple way to lay out the components that you're building.

104
00:06:40,820 --> 00:06:45,440
And this packer has a number of options. So if we scroll down,

105
00:06:45,440 --> 00:06:50,440
you can see that we can change the expand to a boolean, so true or false.

106
00:06:50,900 --> 00:06:55,340
And that tells it whether if it should expand to take up the entire space or

107
00:06:55,340 --> 00:06:58,910
stay as small as it can be. Alternatively,

108
00:06:58,910 --> 00:07:01,730
you have things like side which changes

109
00:07:01,730 --> 00:07:05,750
which side should it be packed to, on the left, right, top, or bottom?

110
00:07:06,710 --> 00:07:10,040
So we know that currently our label is bang in the middle,

111
00:07:10,340 --> 00:07:15,340
but if I change the side to equal left,

112
00:07:16,580 --> 00:07:18,080
now when I run the code,

113
00:07:18,110 --> 00:07:21,590
you can see it's now been packed to the left of the screen.

114
00:07:21,920 --> 00:07:24,800
And if I say bottom,

115
00:07:26,000 --> 00:07:30,680
then it's going to go to the bottom and right and top. All of those will work.

116
00:07:31,790 --> 00:07:34,370
And if I change expand to true,

117
00:07:34,400 --> 00:07:38,330
you can see it's now trying to take up the entire height and width of the

118
00:07:38,330 --> 00:07:41,240
available screen size. Now,

119
00:07:41,240 --> 00:07:43,850
when you're writing this pack function here,

120
00:07:44,510 --> 00:07:48,290
as soon as I type pack and I open up my parentheses,

121
00:07:48,710 --> 00:07:53,570
normally we're used to a whole bunch of properties being listed in here for us,

122
00:07:53,900 --> 00:07:57,800
and we can specify each one and pick and choose basically.

123
00:07:58,400 --> 00:08:01,130
But in this case, this is all that we see.

124
00:08:01,310 --> 00:08:06,310
So what's going on here? Compare this to when we used our turtle module and I'm

125
00:08:07,460 --> 00:08:11,810
going to build a turtle Tim turtle.turtle.

126
00:08:13,040 --> 00:08:16,340
And then I say, tim.write. Look at how,

127
00:08:16,370 --> 00:08:19,910
when I opened up the parentheses here, you can see there's the argument

128
00:08:19,910 --> 00:08:23,360
which is the Object that's going to be written, and then whether

129
00:08:23,360 --> 00:08:26,960
if the writing should be moved to the bottom corner,

130
00:08:27,350 --> 00:08:29,000
which direction it should be aligned,

131
00:08:29,000 --> 00:08:34,000
what's the font and all of the available inputs that we can give to this write

132
00:08:34,580 --> 00:08:39,140
function. Now compare that to our pack function here.

133
00:08:39,830 --> 00:08:43,520
This, you'll notice, is really different. Now,

134
00:08:43,549 --> 00:08:47,930
if you actually look in the documentation for pack,

135
00:08:48,170 --> 00:08:51,980
you can see it actually has a number of things that we can change; before,

136
00:08:51,980 --> 00:08:56,190
expand, fill, in, side, and all of these things

137
00:08:56,460 --> 00:09:00,090
which are modifiable and do various things.

138
00:09:00,480 --> 00:09:04,950
But how is it that it doesn't seem to exist as one of the arguments that we can

139
00:09:04,950 --> 00:09:08,820
use? The secret lies in this last part here,

140
00:09:08,880 --> 00:09:13,230
the **kw. And in the next lesson,

141
00:09:13,530 --> 00:09:17,910
we're going to talk about some more advanced features of Python arguments.

142
00:09:18,270 --> 00:09:22,020
And we're going to look at how we can provide default argument values,

143
00:09:22,320 --> 00:09:27,320
how we can define unlimited positional arguments and unlimited keyword arguments.

144
00:09:28,260 --> 00:09:30,990
To understand this better, head over to the next lesson

145
00:09:31,260 --> 00:09:34,290
and we'll do a deep dive on Python arguments.

