1
00:00:00,330 --> 00:00:01,589
Now in the last lesson,

2
00:00:01,650 --> 00:00:05,670
we managed to get our password manager to start saving the data that we're

3
00:00:05,670 --> 00:00:08,189
entering inside the password manager.

4
00:00:08,790 --> 00:00:12,810
And we formatted it and put it inside our data.txt.

5
00:00:13,590 --> 00:00:13,860
Now,

6
00:00:13,860 --> 00:00:18,860
one of the things that was a bit of an issue in terms of user experience is

7
00:00:19,380 --> 00:00:24,380
whenever the user actually enters their details into this password manager at the

8
00:00:25,590 --> 00:00:26,423
moment,

9
00:00:26,460 --> 00:00:30,900
it doesn't actually give you some sort of way of knowing that it was successful

10
00:00:30,990 --> 00:00:31,890
or if it was not,

11
00:00:32,340 --> 00:00:37,110
or actually getting them to check and verify that the data that they've entered

12
00:00:37,410 --> 00:00:41,010
is definitely the one that they want to save into the file.

13
00:00:41,820 --> 00:00:43,080
In order to do this,

14
00:00:43,170 --> 00:00:46,260
we're going to learn about something else that you can do with tkinter

15
00:00:46,740 --> 00:00:48,960
which is something called standard dialogs.

16
00:00:49,260 --> 00:00:53,460
These are basically popups that your tkinter program can generate.

17
00:00:53,970 --> 00:00:58,350
And one of the most popular standard dialogs are the message boxes.

18
00:00:58,980 --> 00:01:02,520
And it's really, really easy to create these popups.

19
00:01:02,970 --> 00:01:06,390
You can simply just import the message box module

20
00:01:06,660 --> 00:01:11,370
and you can tap into methods like show info or show warning, show error,

21
00:01:11,730 --> 00:01:15,810
or you can ask the user yes no, or OK

22
00:01:15,810 --> 00:01:19,560
or cancel, ask a question yes or no,

23
00:01:19,980 --> 00:01:22,380
ask to retry or cancel.

24
00:01:23,160 --> 00:01:26,790
Now the screenshots where made in a Swedish version of Windows,

25
00:01:26,910 --> 00:01:31,620
but we can actually try these out ourselves and start putting in some standard

26
00:01:31,620 --> 00:01:34,410
dialogs so that we can give the user some feedback.

27
00:01:35,070 --> 00:01:39,090
The thing that we really want to tell the user is the moment that the save

28
00:01:39,090 --> 00:01:43,830
function is called it's because they pressed on the add button here.

29
00:01:44,430 --> 00:01:49,430
But before we actually commit all of the current information to the file,

30
00:01:50,130 --> 00:01:54,060
we want to check and make sure that they're actually happy with what they've

31
00:01:54,060 --> 00:01:54,893
written.

32
00:01:55,110 --> 00:01:59,220
So we can create a message box here and get them to check the details that

33
00:01:59,220 --> 00:02:02,700
they've entered and then hit yes or no as to whether

34
00:02:02,700 --> 00:02:06,300
if they want to save it. In order to use the message box,

35
00:02:06,330 --> 00:02:10,050
we actually have to import the message box. Again,

36
00:02:10,050 --> 00:02:15,050
it's from the tkinter module that we're going to import the message box module.

37
00:02:16,200 --> 00:02:20,280
Now notice how this is not actually a class. So that's why,

38
00:02:20,310 --> 00:02:24,510
even though we've said * which means import everything,

39
00:02:24,870 --> 00:02:29,340
it actually only imports all of the classes, the constants,

40
00:02:29,580 --> 00:02:32,340
but it doesn't import things like the message box,

41
00:02:32,580 --> 00:02:36,300
which is simply just another module of code. And in fact,

42
00:02:36,300 --> 00:02:41,010
if you right-click on it and go to implementation here,

43
00:02:41,430 --> 00:02:46,430
then you can actually see this messagebox.py file and how it's implemented.

44
00:02:49,110 --> 00:02:53,460
The thing that's important though is to actually put it into use. So we can use the

45
00:02:53,460 --> 00:02:57,210
message box and call a method to show a popup.

46
00:02:57,750 --> 00:03:00,160
The simplest type is just showinfo

47
00:03:00,280 --> 00:03:02,110
which you only need to think about two things

48
00:03:02,110 --> 00:03:06,430
really; a title and also a message.

49
00:03:07,420 --> 00:03:10,720
So I'm just going to put title as title and message as message.

50
00:03:11,230 --> 00:03:13,480
And you can see when I press add,

51
00:03:13,630 --> 00:03:18,490
I get this up and it's just got the title and the message and you can't do

52
00:03:18,490 --> 00:03:20,290
anything other than click OK.

53
00:03:22,000 --> 00:03:23,770
In addition to showinfo,

54
00:03:23,980 --> 00:03:27,850
there is actually a lot of other methods; you can ask yes or no,

55
00:03:28,240 --> 00:03:33,160
ask retry or cancel. You can show an error or you can show a warning.

56
00:03:33,910 --> 00:03:36,910
Now in addition, you can actually ask the user something.

57
00:03:36,910 --> 00:03:41,770
So this generates two buttons, yes or no, retry or cancel,

58
00:03:41,800 --> 00:03:46,480
OK or cancel, ask question is also yes and no, and we've also got yes,

59
00:03:46,480 --> 00:03:51,190
no and cancel here. What I'm going to do is I'm going to ask OK

60
00:03:51,190 --> 00:03:52,090
or cancel.

61
00:03:53,110 --> 00:03:58,110
And what I want to ask the user is I'm going to set the title as the website

62
00:03:58,960 --> 00:03:59,920
that they've entered

63
00:04:00,490 --> 00:04:05,490
so that's going to come from here, and then I'm going to set the message as a

64
00:04:06,900 --> 00:04:07,690
f-string.

65
00:04:07,690 --> 00:04:12,690
So I'm going to say these are the details entered:\n.

66
00:04:14,590 --> 00:04:17,560
And then I'll give them the email that they entered

67
00:04:19,180 --> 00:04:24,180
and then a new line and the password that they entered.

68
00:04:28,000 --> 00:04:32,800
And then after new line, I'm going to ask them, is it okay to 

69
00:04:35,010 --> 00:04:35,130
save?

70
00:04:35,130 --> 00:04:37,050
If I go ahead and run this code,

71
00:04:38,130 --> 00:04:41,400
you can see that when I type a website

72
00:04:41,460 --> 00:04:44,760
say Amaazon

73
00:04:45,690 --> 00:04:49,620
and I type in my password and I click add,

74
00:04:50,040 --> 00:04:53,700
I get my popup and it says these are the details entered. This is email,

75
00:04:53,700 --> 00:04:56,130
this is the password, is it okay to save?

76
00:04:56,640 --> 00:04:59,550
Now what I want to do is when they click OK

77
00:04:59,790 --> 00:05:04,350
then for that action to go through and for us to write to our data file.

78
00:05:04,650 --> 00:05:06,360
But if they click cancel,

79
00:05:06,360 --> 00:05:10,260
I want them to be able to go back to the screen and edit it if necessary.

80
00:05:10,920 --> 00:05:12,150
In order to do that,

81
00:05:12,360 --> 00:05:16,020
we have to receive the output from this method call,

82
00:05:16,350 --> 00:05:19,770
which is going to be a boolean. It's going to be either true or false.

83
00:05:19,920 --> 00:05:22,140
So I'm going to set that to is_ok.

84
00:05:23,070 --> 00:05:27,570
Now if this is_ok  is true, well, in that case,

85
00:05:27,600 --> 00:05:30,510
we're going to go ahead and do all of this. So write

86
00:05:30,510 --> 00:05:34,440
to file and delete everything that's inside the entry.

87
00:05:34,890 --> 00:05:38,850
But if it's not okay, then we're simply just going to do nothing

88
00:05:39,120 --> 00:05:42,600
and the popup box will dismiss itself. Now,

89
00:05:42,630 --> 00:05:47,630
if I run this code again and I type in some ### and I click add,

90
00:05:48,750 --> 00:05:53,610
you can see if I click cancel, nothing happens, and I go back. But if I click

91
00:05:53,700 --> 00:05:54,540
OK,

92
00:05:54,750 --> 00:05:59,750
then all of that information is going to be to my data.txt like here.

93
00:06:01,910 --> 00:06:06,910
Now it's time for you to have a go with the message boxes and these standard

94
00:06:07,070 --> 00:06:07,903
dialogs.

95
00:06:08,270 --> 00:06:12,470
But your job is to figure out how can we get our app to have a little bit of

96
00:06:12,470 --> 00:06:17,390
validation because when they have an empty website and an empty password and

97
00:06:17,390 --> 00:06:18,230
they click add,

98
00:06:18,950 --> 00:06:23,900
you shouldn't really let them save an empty password and an empty website,

99
00:06:23,900 --> 00:06:24,890
right? That's not right.

100
00:06:25,340 --> 00:06:29,930
So we want to check to see if the length of the entries in the website or the

101
00:06:29,930 --> 00:06:33,290
password is zero. And in that case,

102
00:06:33,320 --> 00:06:35,720
we're going to bring up a message box

103
00:06:35,750 --> 00:06:38,120
that's just going to tell them basically that, Hey,

104
00:06:38,450 --> 00:06:42,650
you've left some fields empty. So this is what you're aiming for.

105
00:06:42,710 --> 00:06:46,970
Let's say that you've left the website or the password empty and you click add,

106
00:06:47,360 --> 00:06:51,230
you should get a popup that tells you, Hey, don't leave any of the fields empty.

107
00:06:51,620 --> 00:06:56,620
And this should trigger when either the website or the password is empty.

108
00:06:57,680 --> 00:07:01,190
So have a think about how you might solve that challenge and give it a go.

109
00:07:01,660 --> 00:07:02,493
Okay.

110
00:07:06,640 --> 00:07:10,630
All right. So here's where we get all of the information from our entries.

111
00:07:10,810 --> 00:07:13,360
And this is also a good time to do this validation.

112
00:07:13,930 --> 00:07:16,120
We can use an if statement to check, well,

113
00:07:16,210 --> 00:07:20,980
if this website string has a length of zero,

114
00:07:21,400 --> 00:07:23,770
then that means the user didn't type anything in there.

115
00:07:24,250 --> 00:07:26,770
And we can use an or statement to check well,

116
00:07:26,800 --> 00:07:31,660
if the password also has a length of 0, well,

117
00:07:31,660 --> 00:07:33,910
in that case, they didn't type anything in there either.

118
00:07:34,450 --> 00:07:37,240
Now you can also add validation for the email,

119
00:07:37,240 --> 00:07:40,660
but because we have the email already pre-populated,

120
00:07:40,900 --> 00:07:42,190
there's actually not much point here.

121
00:07:42,790 --> 00:07:46,810
So if either they've left the website blank or the password blank,

122
00:07:47,110 --> 00:07:51,370
then we're going to generate a message box and we're going to use the showinfo

123
00:07:51,370 --> 00:07:55,540
message box. The title is just going to say

124
00:07:55,960 --> 00:07:59,950
Oops, and the message is going to say

125
00:08:00,880 --> 00:08:03,220
Please make sure you haven't left any fields empty.

126
00:08:03,820 --> 00:08:07,270
And now when you run this code, if you've left either of these two empty,

127
00:08:07,480 --> 00:08:10,600
then you are going to get a popup that tells you this.

128
00:08:11,170 --> 00:08:16,060
Now we don't actually want the rest of this code to happen if this is true.

129
00:08:16,480 --> 00:08:21,480
So let's go ahead and add an else statement as well so that we can indent all of

130
00:08:22,300 --> 00:08:26,590
this so that we only continue forward

131
00:08:27,070 --> 00:08:32,070
if in fact, the length of the website or the password is not equal to zero.

132
00:08:33,730 --> 00:08:37,030
So in this case, when they leave it empty, you can say OK

133
00:08:37,090 --> 00:08:40,690
and it just goes back to let you continue entering more details.

134
00:08:42,070 --> 00:08:43,210
Now in the next lesson,

135
00:08:43,240 --> 00:08:47,380
we're going to add another piece of functionality to our application

136
00:08:47,680 --> 00:08:49,480
which is the generate password.

137
00:08:49,780 --> 00:08:53,830
We've already seen how terrible the passwords are that we come up with from our

138
00:08:53,830 --> 00:08:54,610
own heads.

139
00:08:54,610 --> 00:08:58,900
So we're going to be using the generate password code that we created many,

140
00:08:58,900 --> 00:09:01,840
many moons ago as one of our projects

141
00:09:01,930 --> 00:09:04,840
and we're going to be embedding that into this application.

142
00:09:05,350 --> 00:09:08,380
So for all of that and more, I'll see you on the next lesson.

