Now we're at the final stretch. Our Pomodoro program is almost complete.
We've got this beautiful user interface,
we've got beautiful colors from our colorhunt.io color palette,
and we've got pretty much everything functioning other than just some loose
ends.
So the first thing I want to fix is the section with the check marks. So
we've created the checkmarks
which is a label that currently just contains a single checkmark.
Now I'm actually going to cut this checkmark and delete the text here because I
want that label to start out as empty. Now,
the only time when I want that label to get an extra check
mark is when the user has completed a work countdown.
So once they've completed their 25 minute session,
then they should get that checkmark. To do that
I'm going to write the code inside this else statement because every single time a
countdown completes,
it's going to go into this else statement because the count goes down to zero.
Now, currently we're just getting the timer to restart itself and go to the next
session.
But what we want to do here is we want to check to see how many reps we have, and
for every two reps
then that means we've completed one work session because it's work then rest,
then work, then rest. So for every two reps,
we've completed one 25 minute work session.
So here's a challenge for you.
So have a think about how you can use our variable reps to check so that for
every two reps we've completed,
we add a checkmark to this checkmark label.
Pause the video and see if you can complete this challenge,
then test it out and see if you managed to get it right.
Okay.
All right.
So we know that the reps is equal to the number of sessions we've had.
So for example, if we've done for reps,
then that means we've had two work sessions and two breaks.
That means we can basically divide reps by two to get the total number of work
sessions we've done.
What I can do is I can create a temporary variable called mark,
which is just going to be in an empty string to start off.
And then I can create a for loop to loop through a range.
And the range is going to go from zero up to our reps divided by 2.
Now this is giving us a warning at the moment because reps divided by 2 or
anything divided by anything becomes a floating type number.
In our case we want to do is we want to use that math.floor again so that we
can floor this reps divided by 2 to a whole number
and that way we get the correct number of work sessions.
If you want to make this a little bit more clear
the next time you read your code,
you can actually create a work_sessions and we can move this part of our code
over to here.
So the total number of work sessions is equal to the reps divided by 2 and
then rounded down to the nearest integer.
And then we can use that number of work sessions to create our range.
When we've created our range,
we know that this loop is going to run for that many times.
So we can add to our mark. For every single time the loop runs,
I'm going to paste in a checkmark.
You should have that checkmark that you had from a previously,
and you can paste it into here. Now,
once we've completed that loop, then all we need to do is the update
our checkmarks label by calling config and then changing the text to equal our
mark. Now, in this case,
it probably makes sense for that to actually be plural.
So I'm gonna use my refactor -> rename
to just add an 's' to everywhere I'm using this.
Now let's test it out. When I hit start,
it should start counting down. And once that work session completes,
then you'll see the timer go to zero and our first checkmark show up.
And for every work session we complete, we get an extra checkmark
just to nudge us towards working a little bit harder.
The final thing that I want to address is our reset mechanism.
So we have this lovely reset button,
but it's not really doing anything at the moment.
What I'm going to do is create a new function here
which I'll call reset_timer.
And what this reset_timer is going to do is basically it's going to reset all
the checkmarks, reset the text inside the timer,
stop the timer and change this title back to the text that it originally had,
which is just the word timer. Now,
the hardest part about this is actually stopping the timer because what we have
to do is we actually need to tap into that window.after again.
But instead of calling after,
we actually want to call after_cancel. This way
we can actually cancel the timer that we had set up previously.
This is the thing that we want to cancel. And if we want to cancel it,
we have to give it a name and we have to put it inside a variable.
So I'm going to call this my timer.
But because this is going to be a local variable because it's created inside
this particular function, I actually have to bring it out.
I have to make it a global variable, so after create it up here,
but what value do I give it? Well, initially,
it's actually not going to have any value at all.
So it's going to start out as none. And then later on,
I'm going to tap into that global timer in order to get it to hold on to this.
Now, then once I want to reset my timer and cancel it, well
then I can actually tap into this global timer and stop the timer.
So now to actually activate this function, reset_timer,
we have to add it as a command to our reset button.
So where we create our button, I'm gonna add it as a command.
And remember, we have to get rid of the parentheses.
So now if I run my program and I start my timer,
you can see that as soon as I click this reset button,
it stops my timer and it doesn't continue.
So now all we have to do is the add
all of the things that we have to reset, for example, the text in here,
we want that to read 00.
So that was the timer_text from our canvas.
And then the other thing we want to do is the title label.
We want to set that to just read timer.
And finally,
we want to reset our check marks so that once you hit the reset button,
it resets this title label,
the timer text here stops the timer and gets rid of all the checkmarks, like
this. Pause the video and give that a go.
Alright. So the first thing we want to change is our timer text.
And because the timer text is created in the canvas,
we have a slightly different way of changing that.
So we call canvas.itemconfig,
and then pass in the thing that we want to change, which is the timer text.
And then the attribute that we want to change, which is it's text.
And then we change that to just say 00:00,
go back to the beginning. Now the title label is a lot easier to change.
We can say title_label.config,
and then we can configure its text to basically just read timer.
Finally, we're going to reset our checkmarks.
So we tap into our checkmarks' label again,
using config to change the text back to empty.
However, there's still a slight problem with our code.
What happens if while our timer is counting down
I hit the reset button and then hit the start button again?
Did you spot that? Our timer jumped from "Work" to "Break" because our reps are still increasing.
So far we haven't reset the number of reps to zero when we reset the timer.
So all we need to do to fix this bug is add: global reps
reps = 0 to the reset time function.
That's all there is to it! Now if we reset our WORK_MIN to 25, SHORT_BREAK 5, LONG_BREAK 20 or whatever
it is that you want it to be. Then once we hit run, we will end up with our final Pomodoro timer.
And it will be now ready for you to start practicing doing Pomodoros and improving your work and your
productivity.
So I hope you enjoyed making this fully functional tkinter application with me and that I hope you
will modify this, customize it, make it truly your own and be able to show off to us what it is that
you have done. Because this really is a useful application that you can use or you can get your friends
and your family to start using and show off your hard work.