Skip to content

Kivy Clock: How to call args with clock schedule

GitHub Repository

AI Transcript provided by KivyWhisper

So hello, welcome to Kivy School. Now we're going to go to a little bit deeper into clock functions. And what we're going to look at is calling clock functions with arguments. So again, I'm going to go like this. Already, my original example uses a partial argument.

So I will show you this partial argument right here and how it works. Simply put, the basic function again is something like this. But you say self-update specific, no arguments, right? But what is that going to do? So without any arguments, it's very hard to have a function that runs perfectly well without any arguments. So this will run nicely for things that has no arguments. But a lot of the times you want to supply your arguments to the function. And the way to do that is using partial.

Now when you use partial, the syntax is simply you supply the function like this, no parentheses. Why? Because if you go and supply the parentheses, you're actually going to call the function. But a function without the parentheses is the function itself. But when you do that, if partial, you call the function without the parentheses, and then you supply the actual arguments. So that's good, right? But why does that work for our example?

Simply because update specific requires some keyword arguments. And this keyword arguments is going to check for clock ID, and then it's going to check which clock ID is it. Is it going to be singles right here? Is it going to be tens right here? Or is it going to be thirties right here, right? And then it will update the correct numeric property accordingly.

So that's why it works. But there's a hidden secret. And the hidden secret is that when you use clock schedule interval, you're also supplying another argument. And a hidden argument in here is going to be DT, which is time. Sorry, I had forgotten to remove the generic one. Okay, let's continue. Okay, so let's start timer. And then let's check what self like this, what's args right here. And then what's keyword args right here.

As you can see, what is this hidden argument? The hidden argument is DT, because I have supplied on the first timer that will run right here, I've supplied a one second delay, right between each interval. And that hidden argument is DT, right? Because in this example, I've had star args, I can accept any amount of positional arguments. And I have accepted DT by writing star args, right?

But let's say your function is not under your control. Let's say you've imported it from some library or some module, and you cannot or don't want to manually update this function, right? And the function simply has no star args, right? It does not accept an arbitrary amount of positional arguments, right? So what do you do? Well, if that's the case, then you have two things, right? You can just add it if you have access. If you do not have access to the function, and you cannot just add star args to accept any number of positional arguments, you can simply use lambda.

Lambdas are other solution to calling these clock functions with arguments. So let's go up here. Let's undo any changes that mean, okay, where's my lambda example, right? So let's read the lambda example notes. So star args is an easy way to accept the DT argument that is provided when using clock. This function does not have star args, as you can see, and will fail if you try using clock schedule and partial. If you do not have access to the function, it's from some library that you don't want to modify.

The simple answer is to use a lambda that passes DT as a variable. So as you can see here, I'm going to use failed lambda that will crash the app. And it's going to be right here. And I said, okay, right, if you have a clock schedule interval, and you want to just say, okay, I want to use this function with my arguments, let's use partial, right? I'm going to use it right here.

I'm going to use partial lambda example, clock ID 30. And in lambda example, it does check clock ID, it does check if the ID is 30. And it does increase the 30 seconds numeric property plus one, right? But is it going to work? It's not going to work because we are supplying a DT argument and lambda example does not accept it. We're going to press like this, and it's going to crash the app in one second. Why? Type error, custom box lambda example takes one position argument, but two were given, right?

It's not accepting the DT argument. So let's go again. And then let's try the working lambda example that increases 30 seconds, right? So as you can see here, again, lambda pass and lambda failed, they're both run here. Failed lambda example that will crash app.

Working lambda example that increases 30 seconds. So we will run it right now. Right? And in one second, it will increase it two seconds, increase it three seconds, increase it. Why does it work? Because I've used the lambda function. I simply say lambda, this is our variable and then colon to define our function.

And it's simply going to be self lambda example, clock ID 30. I'm actually calling this function. And because I have not placed DT, DT in here is kind of like a dummy variable. And we've just removed it from calling this function because the function doesn't require it. So that's one way to call this function and then handle DTs. You can simply either throw it away using a lambda example, or modify the function itself with star art like in here. Okay.

So I've called, I've done with calling clock function as arguments, this and like this and like this. If you want to schedule a function that doesn't accept the DT argument, you use a lambda. But here's a problem. And again, from Kivy org documentation, it says right here, you cannot unschedule an anonymous function unless you keep a reference to it.

It's better to add star args to your function definition so that it can be called with an arbitrary number of parameters. Okay, so again, I've gone through partial, I've shown you how to use it, which is the recommended. And then I've gone through lambda when function does not accept DT. And if you run this, most likely it's going to end up running forever, unless you add a reference to it and then say, okay, you call the reference dot cancel.

So thank you for watching. This has been Kivy School.

Article Error Reporting

Message @BadMetrics on the Kivy Discord.