Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Polling7:03 with Kenneth Love
We can start and stop the timer so now we need to update the timer label when each second ticks down.
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.
All right so to do my polling, I'm actually gonna make a new method and 0:00 I'm gonna call it update. 0:03 And let's just print updated. 0:06 All right and 0:09 then we want to make sure that our update method gets called every second. 0:10 And what's cool is that our global app, the thing we called master, 0:17 in here self.master, I know we called it root on the outside. 0:22 It has a method called after and 0:25 it will fire off after a certain number of milliseconds. 0:30 So let's use that. 0:34 So we'll say self.master.after and then, 0:35 we're supposed to find the number of milliseconds. 0:40 So 1000 milliseconds is one second. 0:42 And then we want that to run self.update. 0:46 Okay, so we had 1000 milliseconds, or one second, and then we run update again. 0:50 All right, so after all the stuff gets built, then we run self.update. 0:55 Okay, so now run. 1:02 And every second, the window is off screen. 1:07 Every second, it prints out updated. 1:09 Cool so, that's great. 1:12 We know that works. 1:15 In this method, is where I want to decrement the timer. 1:16 So first let's get how much time is left. 1:20 And we'll say time_left = self.time_left.get() so 1:24 we're just holding onto it. 1:29 That's just easier to type than self.time_left all the time. 1:30 So for running, and there's time left, so if self.running and 1:34 time_left so time left isn't zero, 1:40 then we want to take one second off of the number. 1:45 So, we'll do self.time and left.set to time left -1. 1:49 Otherwise, so 1:55 else, we just wanna set running to false and then reset the buttons. 1:58 So, we can just call self.stop_timer. 2:02 Because that does what we want. 2:07 It turns running to false and resets the buttons. 2:08 So if we're running and there's time left. 2:12 Let's take a second off the time left number. 2:14 Otherwise let's just call our stop timer method to reset the buttons and 2:16 turn off the running variable. 2:20 All right so now we need to make sure that our timer, like we haven't even been 2:22 showing the timer label at all, it's just been blank because there's no text. 2:26 So we need to make sure that updates with how much time is actually left. 2:30 So we should tell the variable that controls the text of the timer label 2:35 to watch for changes. 2:39 And then when it changes to redraw the label. 2:41 So now we're already drawing the label right here in our build timer method. 2:44 We need to tell the method, since we're gonna, so 2:51 first of all let's come up here, let's do this one step at a time. 2:54 All right so here is where we made our timer text variable. 2:58 So this is the text that we display. 3:06 So we wanna tell this text that whenever 3:12 it gets updated to go do some of it. 3:17 All right, so w means whenever this was written to. 3:22 Then we wanna call self.build_timer. 3:27 Okay, so go down here to self.build_timer. 3:31 So build_timer is now gonna get called by itself. 3:35 But it's also gonna get called whenever that trace fires. 3:39 And when that trace fires, it sends some more information as well. 3:42 So we have to accept args. 3:46 We're not going to use these args at all we don't care about them but 3:49 we have to except them because python has the rule that like you have to know 3:54 how many variables are coming in. 3:57 So anytime the timer updates were going to call build timer. 4:00 Any time the timer text updates okay. 4:04 So, let's go update that label. 4:08 So here in update we are going to the minutes and seconds that are left. 4:11 So I'm actually going to make another method here. 4:20 I'm gonna call it minutes_seconds. 4:24 And it's going to take seconds which is a going to be a number of seconds. 4:27 And I want to return the minutes which should be the int version 4:31 of seconds divided by 60. 4:35 I could also do seconds // 60 which will do int division. 4:37 And then I want the int version of seconds modulo 60. 4:43 Modulo gives us effectively the remainder. 4:47 So we're gonna return minutes. 4:50 And we're gonna return seconds. 4:52 All right, so down here, inside the time left, let's do minutes, 4:55 seconds = self.minutes_second. 5:00 And we're gonna pass in (time_left) cuz that's how much time is left, right? 5:04 And then we're gonna do self.timer_text.set(). 5:09 And I want to set this timer text to something. 5:13 Normally we would do like this because we want minutes and seconds but 5:17 we want to format this. 5:19 So I want zeros padded out to two digits. 5:20 And then again zeros added out to two digits. 5:25 There we go. 5:30 And then I call format with minutes and seconds. 5:31 All right and then we need to do this again. 5:34 Let's just copy this thing here all over again. 5:40 End it before the timers stop. 5:45 But instead of time left here, we're going to do it with 5:47 DEFAULT_GAP and then we get that same text in there. 5:51 Okay, this could probably be optimized if we wanted to move it in 5:59 out of the if else. 6:03 But for now let's just not worry about it, okay. 6:04 [LAUGH] So that should do the work that we needed it to do. 6:07 And this should make our timer count down. 6:10 Let's see, let's run this. 6:12 Bring one over here, let's hit Start. 6:16 What do you know? 6:21 Look at that, it's counting down by seconds, more or less. 6:22 All right let's test what happens when this runs out. 6:26 But to do that, I don't wanna wait 25 minutes. 6:32 So let's do a new default gap and we'll just say, five seconds. 6:35 All right, so run this again. 6:39 And so all right, we've got five seconds left, hit start, four, three, two, one. 6:46 Boop, and it, hey, look at that. 6:54 It reset. 6:55 The stop button is effectively clicked, gonna start, gonna go back to five. 6:56 So that's cool, that's awesome. 7:00 Now, let's see about setting up an alert. 7:01
You need to sign up for Treehouse in order to download course files.Sign up