Hi friends!
I want to create a countdown program. Here is my code:

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

import datetime

class CountdownApp(App):
    def build(self):

        delta = datetime.datetime(2015, 3, 21, 2, 15, 11) - datetime.datetime.now()
        days = delta.days
        days = str(days)
        self.label_days = Label(text=days + "  days")

        hour_string = str(delta).split(', ')[1]
        hours = hour_string.split(':')[0]
        self.label_hours = Label(text=hours + "  hours")

        minuts = hour_string.split(':')[1]
        self.label_minuts = Label(text=minuts + "  minuts")

        seconds = hour_string.split(':')[2]
        self.label_seconds = Label(text=seconds + "  seconds")



        b = BoxLayout(orientation="vertical")
        b.add_widget(self.label_days)
        b.add_widget(self.label_hours)
        b.add_widget(self.label_minuts)
        b.add_widget(self.label_seconds)
        return b

if __name__ == "__main__":
    CountdownApp().run()

I want the program to update itself every seconds and then the label wich shows seconds should be updated every seconds....
How can i do that?

Recommended Answers

All 5 Replies

you could try to use kivy's Clock module

from kivy.clock import Clock

class CountdownApp(App):
    def build(self):
        t = 1.0 / 60.0 # 1/60th of a second, use 1.0 instead to update every second
        Clock.schedule_interval(self.update, t)

        # some other code ....

    def update(self, dt):
        # update things - your labels?

@Slyte, what is that dt in line 10 for?

I refer you to this Kivy Clock. :) I'm not really well versed with kivy. I do know dt means delta-time. :)

Well, i stil have problem with this program, what should i type in line 11, for the update function?!

All my code that should be updated every seconds, are in the build function in the CountdownApp class.

the easiest way would be wraping the part you want in a while True loop and then putting a time.sleep(1) at the start of that loop or at the end of that so it will make a 1 second delay to update the loop , so your countdown .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.