Now in the last lesson,
we managed to get our password manager to start saving the data that we're
entering inside the password manager.
And we formatted it and put it inside our data.txt.
Now,
one of the things that was a bit of an issue in terms of user experience is
whenever the user actually enters their details into this password manager at the
moment,
it doesn't actually give you some sort of way of knowing that it was successful
or if it was not,
or actually getting them to check and verify that the data that they've entered
is definitely the one that they want to save into the file.
In order to do this,
we're going to learn about something else that you can do with tkinter
which is something called standard dialogs.
These are basically popups that your tkinter program can generate.
And one of the most popular standard dialogs are the message boxes.
And it's really, really easy to create these popups.
You can simply just import the message box module
and you can tap into methods like show info or show warning, show error,
or you can ask the user yes no, or OK
or cancel, ask a question yes or no,
ask to retry or cancel.
Now the screenshots where made in a Swedish version of Windows,
but we can actually try these out ourselves and start putting in some standard
dialogs so that we can give the user some feedback.
The thing that we really want to tell the user is the moment that the save
function is called it's because they pressed on the add button here.
But before we actually commit all of the current information to the file,
we want to check and make sure that they're actually happy with what they've
written.
So we can create a message box here and get them to check the details that
they've entered and then hit yes or no as to whether
if they want to save it. In order to use the message box,
we actually have to import the message box. Again,
it's from the tkinter module that we're going to import the message box module.
Now notice how this is not actually a class. So that's why,
even though we've said * which means import everything,
it actually only imports all of the classes, the constants,
but it doesn't import things like the message box,
which is simply just another module of code. And in fact,
if you right-click on it and go to implementation here,
then you can actually see this messagebox.py file and how it's implemented.
The thing that's important though is to actually put it into use. So we can use the
message box and call a method to show a popup.
The simplest type is just showinfo
which you only need to think about two things
really; a title and also a message.
So I'm just going to put title as title and message as message.
And you can see when I press add,
I get this up and it's just got the title and the message and you can't do
anything other than click OK.
In addition to showinfo,
there is actually a lot of other methods; you can ask yes or no,
ask retry or cancel. You can show an error or you can show a warning.
Now in addition, you can actually ask the user something.
So this generates two buttons, yes or no, retry or cancel,
OK or cancel, ask question is also yes and no, and we've also got yes,
no and cancel here. What I'm going to do is I'm going to ask OK
or cancel.
And what I want to ask the user is I'm going to set the title as the website
that they've entered
so that's going to come from here, and then I'm going to set the message as a
f-string.
So I'm going to say these are the details entered:\n.
And then I'll give them the email that they entered
and then a new line and the password that they entered.
And then after new line, I'm going to ask them, is it okay to
save?
If I go ahead and run this code,
you can see that when I type a website
say Amaazon
and I type in my password and I click add,
I get my popup and it says these are the details entered. This is email,
this is the password, is it okay to save?
Now what I want to do is when they click OK
then for that action to go through and for us to write to our data file.
But if they click cancel,
I want them to be able to go back to the screen and edit it if necessary.
In order to do that,
we have to receive the output from this method call,
which is going to be a boolean. It's going to be either true or false.
So I'm going to set that to is_ok.
Now if this is_ok is true, well, in that case,
we're going to go ahead and do all of this. So write
to file and delete everything that's inside the entry.
But if it's not okay, then we're simply just going to do nothing
and the popup box will dismiss itself. Now,
if I run this code again and I type in some ### and I click add,
you can see if I click cancel, nothing happens, and I go back. But if I click
OK,
then all of that information is going to be to my data.txt like here.
Now it's time for you to have a go with the message boxes and these standard
dialogs.
But your job is to figure out how can we get our app to have a little bit of
validation because when they have an empty website and an empty password and
they click add,
you shouldn't really let them save an empty password and an empty website,
right? That's not right.
So we want to check to see if the length of the entries in the website or the
password is zero. And in that case,
we're going to bring up a message box
that's just going to tell them basically that, Hey,
you've left some fields empty. So this is what you're aiming for.
Let's say that you've left the website or the password empty and you click add,
you should get a popup that tells you, Hey, don't leave any of the fields empty.
And this should trigger when either the website or the password is empty.
So have a think about how you might solve that challenge and give it a go.
Okay.
All right. So here's where we get all of the information from our entries.
And this is also a good time to do this validation.
We can use an if statement to check, well,
if this website string has a length of zero,
then that means the user didn't type anything in there.
And we can use an or statement to check well,
if the password also has a length of 0, well,
in that case, they didn't type anything in there either.
Now you can also add validation for the email,
but because we have the email already pre-populated,
there's actually not much point here.
So if either they've left the website blank or the password blank,
then we're going to generate a message box and we're going to use the showinfo
message box. The title is just going to say
Oops, and the message is going to say
Please make sure you haven't left any fields empty.
And now when you run this code, if you've left either of these two empty,
then you are going to get a popup that tells you this.
Now we don't actually want the rest of this code to happen if this is true.
So let's go ahead and add an else statement as well so that we can indent all of
this so that we only continue forward
if in fact, the length of the website or the password is not equal to zero.
So in this case, when they leave it empty, you can say OK
and it just goes back to let you continue entering more details.
Now in the next lesson,
we're going to add another piece of functionality to our application
which is the generate password.
We've already seen how terrible the passwords are that we come up with from our
own heads.
So we're going to be using the generate password code that we created many,
many moons ago as one of our projects
and we're going to be embedding that into this application.
So for all of that and more, I'll see you on the next lesson.