Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Alert!3:23 with Kenneth Love
Finally, we need to show an alert window when the timer runs out.
The official docs for Tkinter aren't the best in the world but they'll get you started. The documentation from effbot hasn't been updated in about a decade but it's actually still mostly accurate. These are my two main resources for learning Tkinter and preparing this workshop.
Terms and things
Tk() - A Tkinter application. This is what is responsible for the window or application you see on your screen. You'll end up binding most things to this.
mainloop() - The loop that keeps your Tkinter app running. This loop runs forever.
Frame - An invisible box that you can put things in.
Label - A piece of text on the screen.
Button - A button (whodathunk?!)
StringVar - A variable that holds onto a string.
IntVar - A variable that holds onto an int.
trace() - A way to follow the changes and access of a variable.
rowconfigure() - Methods to configure the number and weight of columns and rows.
pack() - Method to insert a widget in the available space.
And TkInter has one.
But we can't use it from TkInter.whatever.
We have to import it on its own.
So, we'll say from tkinter import messagebox.
And there are a lot of different message boxes.
Some ask a question and the user can answer with a yes or no.
Others accept input, like a text box to take in information.
And some, like the one that we're gonna use, are just for sharing information.
So, it's just going to put out a message.
So let's go down here.
Let's make a new function.
And we're going to call it alert cuz it's our alert.
And since it's going to get triggered, as well, we need it to take args.
So, we're gonna say, if not self.time left.get().
So, if there are no seconds left, then we want to do message box.showinfo().
And then the title that we want on the window,
this right here says pymodoro.pi gui blah, blah, blah.
We want that to say Timer done.
I suppose it could say Pymodoro or something.
And then our message is just going to be, Your timer is done!
Super, super exciting [LAUGH] there.
Okay, but now, how do I want to fire this off?
So, we need to actually watch for stuff to happen and then fire this off.
And this really make sense to have fired whenever this runs out.
Now, I don't want to do it and update because that's weird.
I've had to watch for running conditions and make sure the start button is disabled
or the stop button is enabled, and then, it's just a lot of trouble.
So, let's not worry about that.
Let's do it with another trace.
So, if we come back up here to our init, and we have this time left.
Let's do self.time_left.trace, and
we want whenever this is written to, to call self.alert.
So, whenever our update method changes the number of seconds that are left,
our alert method checks to see if it should fire the message box.
Let's try this out.
And I have a feeling this may end up firing off a second or
two early due to the order of stuff.
But not that big of a deal, especially not in something that we're just like,
[NOISE] let's see what we can build.
Feel free to experiment and see if you can make it not do that.
But let's see, so four, three, two, one.
Our timer's done, cool.
So, yeah, you can see it's about a second early,
it fires off, but not that big of a deal.
So, there you go.
In just a few short videos, and I don't know, 100 lines of code?
Let's see, how many lines did we add here?
114 lines of code, a few short videos, we have built a fully function Pymodoro
application that'll let us do a lot of work very, very quickly.
While our little app might not do much, it gives us a good glimpse of what all we can
do with the Kintor and the rest of Python.
Whether you're building your whole app into Kintor or just using it at a proof
of concept, I think this library will serve you well in the future.
I know people making games in it, and even using TkIntor to control lasers.
I can't wait to see what you do with it.
You need to sign up for Treehouse in order to download course files.Sign up