Skip to content

Kivy Clock Schedule and Unschedule Events

GitHub Repository

AI Transcript provided by KivyWhisper

Hello and welcome back to Kivy School. Today I'm going to be teaching you about Kivy Clock. Now this is going to be a Clock video and in part A we're going to learn how to start a timer, how to unschedule a timer, and very quickly why you should use properties.

So let's start. I'm going to run this Python main.py and then we're going to go and look at the timer that runs only once in five seconds. I'm going to click it and 1000, 2000, 3000, 4000, 5000, and then it will update all of these timers every five seconds but only once. If you notice 1000, 2000, 3000, 4000, 5000, it hasn't run again. It hasn't updated these numbers. Okay so cool. Now we have a timer only once example and then I'm going to show you how it looks like. If we go to timer only once every five seconds, let's look at start timer once. So when we say start timer once, we just say clock schedule once. Our custom function which is going to be update count and then this is going to be the time in seconds.

So in five seconds it will run update count and if we check dev update count all it just says is okay update the seconds numeric property, update the 10 seconds numeric property, update the 30 seconds numeric property, and it's a numeric property right here and it's a kivyproperty, it's important numeric property. Okay so that's once every five seconds.

Now we're going to run timer every one second, 10 seconds, and 30 seconds. I'm going to start a timer and as I talk you're going to see the numbers are going to go up and then I'm going to show you exactly what it looks like. So how are you going to run this kind of recurring timer?

The recurring timer requires a couple things. Number one it requires kivyclock import clock, the top right here. Then we're going to say self.attribute clock schedule interval and then that's going to look like this. If I can find it, start timers and as you can see this attribute of the custom box layout self.single is equal to clock.schedule interval, our function, and the time. And then self.10 is schedule interval, our function, and the time. And again our function and the time. So that is how you start some timers.

Now you're going to see there's some partials. I'll cover it later but you can very easily without any arguments you could just do if you don't have to add supply any specific arguments you can just go like this. Self.update specific and then time. And then it will call this function right here repeatedly every x amount of seconds however much you want and it will keep running it. Now the reason I'm doing some partials is for part b of this video which is going to be about when you need to use partials to supply an argument and I will show you lambdas when you have a function that cannot accept dt which is one of the arguments that is applied to your functions when you use a clock schedule interval.

So you have a schedule interval at this time with partial and the same function run without partial but it will have no arguments so that's something to keep in mind. So you run a function using this repeatedly and then you run a function with arguments using this repeatedly.

Now another question is okay how do you unschedule these functions right? How would you unschedule it? Well simply put you would say okay I'm gonna press this button called cancel timer and if you see the seconds are gonna stop right? At the function cancel all timers you're simply going to call this reference to our timer and then say dot cancel. That's it very simple because I have saved the reference to the scheduled clock function as an attribute on our class.

Now let's say I had just done this right clock schedule interval. They will just run forever and have no reference to it and I can never cancel it but because I said okay self dot single so the single attribute of this class is the schedule interval I can always call it to say cancel so if you want to cancel a scheduled interval you need to have a reference to it because python is object oriented the easiest way is to just say okay I'm an attribute of our custom box layout class and then say I'm going to call our reference clock schedule dot cancel.

That's how you unschedule your clocks. So I've run through how to start a timer and how to start a timer is like this schedule interval. I've told you to import kivy clock and I've told you that you should always save a reference to your clocks because that's how you're going to call cancel. Okay and then I've told you to save a reference and I've unscheduled all three clock widgets as you can see the times have been stuck because I cancel the timers and then I'll go very quickly on to why use properties.

The seconds are a numeric property 10 seconds and 30 seconds are a numeric property and then to use this just say kivy properties import numeric property. Now this is because in my update function I simply say self seconds plus equal one self 10 seconds plus equal one self 30 seconds plus equal one. Why? Because when you use a property and you update that property it will force a kivy redraw and re-update and will update your kivy widgets without you having to manually update your kivy widget itself using something like you know like button dot text equals self dot 30 seconds. You don't have to do that instead if you use a property you can just use that property update it and it will force a kivy update.

So that's part a done now I'm going to go to part b which is calling clock functions with arguments.

Article Error Reporting

Message @BadMetrics on the Kivy Discord.