WEBVTT

0
00:00.330 --> 00:01.590
Now in the last lesson,

1
00:01.650 --> 00:05.670
we managed to get our password manager to start saving the data that we're

2
00:05.670 --> 00:08.190
entering inside the password manager.

3
00:08.790 --> 00:12.810
And we formatted it and put it inside our data.txt.

4
00:13.590 --> 00:13.860
Now,

5
00:13.860 --> 00:18.860
one of the things that was a bit of an issue in terms of user experience is

6
00:19.380 --> 00:24.380
whenever the user actually enters their details into this password manager at the

7
00:25.590 --> 00:26.423
moment,

8
00:26.460 --> 00:30.900
it doesn't actually give you some sort of way of knowing that it was successful

9
00:30.990 --> 00:31.890
or if it was not,

10
00:32.340 --> 00:37.110
or actually getting them to check and verify that the data that they've entered

11
00:37.410 --> 00:41.010
is definitely the one that they want to save into the file.

12
00:41.820 --> 00:43.080
In order to do this,

13
00:43.170 --> 00:46.260
we're going to learn about something else that you can do with tkinter

14
00:46.740 --> 00:48.960
which is something called standard dialogs.

15
00:49.260 --> 00:53.460
These are basically popups that your tkinter program can generate.

16
00:53.970 --> 00:58.350
And one of the most popular standard dialogs are the message boxes.

17
00:58.980 --> 01:02.520
And it's really, really easy to create these popups.

18
01:02.970 --> 01:06.390
You can simply just import the message box module

19
01:06.660 --> 01:11.370
and you can tap into methods like show info or show warning, show error,

20
01:11.730 --> 01:15.810
or you can ask the user yes no, or OK

21
01:15.810 --> 01:19.560
or cancel, ask a question yes or no,

22
01:19.980 --> 01:22.380
ask to retry or cancel.

23
01:23.160 --> 01:26.790
Now the screenshots where made in a Swedish version of Windows,

24
01:26.910 --> 01:31.620
but we can actually try these out ourselves and start putting in some standard

25
01:31.620 --> 01:34.410
dialogs so that we can give the user some feedback.

26
01:35.070 --> 01:39.090
The thing that we really want to tell the user is the moment that the save

27
01:39.090 --> 01:43.830
function is called it's because they pressed on the add button here.

28
01:44.430 --> 01:49.430
But before we actually commit all of the current information to the file,

29
01:50.130 --> 01:54.060
we want to check and make sure that they're actually happy with what they've

30
01:54.060 --> 01:54.893
written.

31
01:55.110 --> 01:59.220
So we can create a message box here and get them to check the details that

32
01:59.220 --> 02:02.700
they've entered and then hit yes or no as to whether

33
02:02.700 --> 02:06.300
if they want to save it. In order to use the message box,

34
02:06.330 --> 02:10.050
we actually have to import the message box. Again,

35
02:10.050 --> 02:15.050
it's from the tkinter module that we're going to import the message box module.

36
02:16.200 --> 02:20.280
Now notice how this is not actually a class. So that's why,

37
02:20.310 --> 02:24.510
even though we've said * which means import everything,

38
02:24.870 --> 02:29.340
it actually only imports all of the classes, the constants,

39
02:29.580 --> 02:32.340
but it doesn't import things like the message box,

40
02:32.580 --> 02:36.300
which is simply just another module of code. And in fact,

41
02:36.300 --> 02:41.010
if you right-click on it and go to implementation here,

42
02:41.430 --> 02:46.430
then you can actually see this messagebox.py file and how it's implemented.

43
02:49.110 --> 02:53.460
The thing that's important though is to actually put it into use. So we can use the

44
02:53.460 --> 02:57.210
message box and call a method to show a popup.

45
02:57.750 --> 03:00.160
The simplest type is just showinfo

46
03:00.280 --> 03:02.110
which you only need to think about two things

47
03:02.110 --> 03:06.430
really; a title and also a message.

48
03:07.420 --> 03:10.720
So I'm just going to put title as title and message as message.

49
03:11.230 --> 03:13.480
And you can see when I press add,

50
03:13.630 --> 03:18.490
I get this up and it's just got the title and the message and you can't do

51
03:18.490 --> 03:20.290
anything other than click OK.

52
03:22.000 --> 03:23.770
In addition to showinfo,

53
03:23.980 --> 03:27.850
there is actually a lot of other methods; you can ask yes or no,

54
03:28.240 --> 03:33.160
ask retry or cancel. You can show an error or you can show a warning.

55
03:33.910 --> 03:36.910
Now in addition, you can actually ask the user something.

56
03:36.910 --> 03:41.770
So this generates two buttons, yes or no, retry or cancel,

57
03:41.800 --> 03:46.480
OK or cancel, ask question is also yes and no, and we've also got yes,

58
03:46.480 --> 03:51.190
no and cancel here. What I'm going to do is I'm going to ask OK

59
03:51.190 --> 03:52.090
or cancel.

60
03:53.110 --> 03:58.110
And what I want to ask the user is I'm going to set the title as the website

61
03:58.960 --> 03:59.920
that they've entered

62
04:00.490 --> 04:05.490
so that's going to come from here, and then I'm going to set the message as a

63
04:06.900 --> 04:07.690
f-string.

64
04:07.690 --> 04:12.690
So I'm going to say these are the details entered:\n.

65
04:14.590 --> 04:17.560
And then I'll give them the email that they entered

66
04:19.180 --> 04:24.180
and then a new line and the password that they entered.

67
04:28.000 --> 04:32.800
And then after new line, I'm going to ask them, is it okay to 

68
04:35.010 --> 04:35.130
save?

69
04:35.130 --> 04:37.050
<v 0>If I go ahead and run this code,</v>

70
04:38.130 --> 04:41.400
you can see that when I type a website

71
04:41.460 --> 04:44.760
say Amaazon

72
04:45.690 --> 04:49.620
and I type in my password and I click add,

73
04:50.040 --> 04:53.700
I get my popup and it says these are the details entered. This is email,

74
04:53.700 --> 04:56.130
this is the password, is it okay to save?

75
04:56.640 --> 04:59.550
Now what I want to do is when they click OK

76
04:59.790 --> 05:04.350
then for that action to go through and for us to write to our data file.

77
05:04.650 --> 05:06.360
But if they click cancel,

78
05:06.360 --> 05:10.260
I want them to be able to go back to the screen and edit it if necessary.

79
05:10.920 --> 05:12.150
In order to do that,

80
05:12.360 --> 05:16.020
we have to receive the output from this method call,

81
05:16.350 --> 05:19.770
which is going to be a boolean. It's going to be either true or false.

82
05:19.920 --> 05:22.140
So I'm going to set that to is_ok.

83
05:23.070 --> 05:27.570
Now if this is_ok  is true, well, in that case,

84
05:27.600 --> 05:30.510
we're going to go ahead and do all of this. So write

85
05:30.510 --> 05:34.440
to file and delete everything that's inside the entry.

86
05:34.890 --> 05:38.850
But if it's not okay, then we're simply just going to do nothing

87
05:39.120 --> 05:42.600
and the popup box will dismiss itself. Now,

88
05:42.630 --> 05:47.630
if I run this code again and I type in some ### and I click add,

89
05:48.750 --> 05:53.610
you can see if I click cancel, nothing happens, and I go back. But if I click

90
05:53.700 --> 05:54.540
OK,

91
05:54.750 --> 05:59.750
then all of that information is going to be to my data.txt like here.

92
06:01.910 --> 06:06.910
Now it's time for you to have a go with the message boxes and these standard

93
06:07.070 --> 06:07.903
dialogs.

94
06:08.270 --> 06:12.470
But your job is to figure out how can we get our app to have a little bit of

95
06:12.470 --> 06:17.390
validation because when they have an empty website and an empty password and

96
06:17.390 --> 06:18.230
they click add,

97
06:18.950 --> 06:23.900
you shouldn't really let them save an empty password and an empty website,

98
06:23.900 --> 06:24.890
right? That's not right.

99
06:25.340 --> 06:29.930
So we want to check to see if the length of the entries in the website or the

100
06:29.930 --> 06:33.290
password is zero. And in that case,

101
06:33.320 --> 06:35.720
we're going to bring up a message box

102
06:35.750 --> 06:38.120
that's just going to tell them basically that, Hey,

103
06:38.450 --> 06:42.650
you've left some fields empty. So this is what you're aiming for.

104
06:42.710 --> 06:46.970
Let's say that you've left the website or the password empty and you click add,

105
06:47.360 --> 06:51.230
you should get a popup that tells you, Hey, don't leave any of the fields empty.

106
06:51.620 --> 06:56.620
And this should trigger when either the website or the password is empty.

107
06:57.680 --> 07:01.190
So have a think about how you might solve that challenge and give it a go.

108
07:01.660 --> 07:02.493
<v 2>Okay.</v>

109
07:06.640 --> 07:10.630
<v 0>All right. So here's where we get all of the information from our entries.</v>

110
07:10.810 --> 07:13.360
And this is also a good time to do this validation.

111
07:13.930 --> 07:16.120
We can use an if statement to check, well,

112
07:16.210 --> 07:20.980
if this website string has a length of zero,

113
07:21.400 --> 07:23.770
then that means the user didn't type anything in there.

114
07:24.250 --> 07:26.770
And we can use an or statement to check well,

115
07:26.800 --> 07:31.660
if the password also has a length of 0, well,

116
07:31.660 --> 07:33.910
in that case, they didn't type anything in there either.

117
07:34.450 --> 07:37.240
Now you can also add validation for the email,

118
07:37.240 --> 07:40.660
but because we have the email already pre-populated,

119
07:40.900 --> 07:42.190
there's actually not much point here.

120
07:42.790 --> 07:46.810
So if either they've left the website blank or the password blank,

121
07:47.110 --> 07:51.370
then we're going to generate a message box and we're going to use the showinfo

122
07:51.370 --> 07:55.540
message box. The title is just going to say

123
07:55.960 --> 07:59.950
Oops, and the message is going to say

124
08:00.880 --> 08:03.220
Please make sure you haven't left any fields empty.

125
08:03.820 --> 08:07.270
And now when you run this code, if you've left either of these two empty,

126
08:07.480 --> 08:10.600
then you are going to get a popup that tells you this.

127
08:11.170 --> 08:16.060
Now we don't actually want the rest of this code to happen if this is true.

128
08:16.480 --> 08:21.480
So let's go ahead and add an else statement as well so that we can indent all of

129
08:22.300 --> 08:26.590
this so that we only continue forward

130
08:27.070 --> 08:32.070
if in fact, the length of the website or the password is not equal to zero.

131
08:33.730 --> 08:37.030
So in this case, when they leave it empty, you can say OK

132
08:37.090 --> 08:40.690
and it just goes back to let you continue entering more details.

133
08:42.070 --> 08:43.210
Now in the next lesson,

134
08:43.240 --> 08:47.380
we're going to add another piece of functionality to our application

135
08:47.680 --> 08:49.480
which is the generate password.

136
08:49.780 --> 08:53.830
We've already seen how terrible the passwords are that we come up with from our

137
08:53.830 --> 08:54.610
own heads.

138
08:54.610 --> 08:58.900
So we're going to be using the generate password code that we created many,

139
08:58.900 --> 09:01.840
many moons ago as one of our projects

140
09:01.930 --> 09:04.840
and we're going to be embedding that into this application.

141
09:05.350 --> 09:08.380
So for all of that and more, I'll see you on the next lesson.