Search code examples
kivykivy-language

kivy Countdown Animation smoothness and synchronization


enter image description here

I have programmed this Countdown Animation in kivy. It has two problems:

  • Circle Animation is not in sync with numbers
  • Circle Animation is not smooth

I would highly appreciate any recommendations to fix either. Thanks

This the code (py)

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.properties import NumericProperty


class CountDownLbl(Label):
    angle = NumericProperty(0)
    startCount = NumericProperty(20)
    Count = NumericProperty()
    def __init__(self, **kwargs):
        super(CountDownLbl, self).__init__(**kwargs)
        Clock.schedule_once(self.set_Circle, 0.1)
        self.Count = self.startCount
        Clock.schedule_interval(lambda x: self.set_Count(), 1)

    def set_Circle(self, dt):
        self.angle = self.angle + dt*360
        if self.angle >= 360:
            self.angle = 0
        Clock.schedule_once(self.set_Circle, 0.1)

    def set_Count(self):
        self.Count = self.Count - 1


class PhotoBoothApp(App):
    pass

if __name__ == '__main__':
    try:
        app = PhotoBoothApp()
        app.run()
    except KeyboardInterrupt:
        app.stop()

(kv)

CountDownLbl:
    text: str(self.Count)
    font_size: 30
    canvas:
        Color:
            rgb: 1,0,1
        Line:
            circle:self.center_x, self.center_y, 90, 0, self.angle
            width: 5

Solution

  • Change your interval to 1.0/360

    Clock.schedule_once(self.set_Circle, 1.0/360)
    

    You could also write it like this:

    class CountDownLbl(Label):
    
        angle = NumericProperty(0)
        startCount = NumericProperty(20)
        Count = NumericProperty()
    
        def __init__(self, **kwargs):
            super(CountDownLbl, self).__init__(**kwargs)
            Clock.schedule_once(self.set_Circle, 0.1)
            self.Count = self.startCount
    
        def set_Circle(self, dt):
            self.angle = self.angle + dt*360
            if self.angle >= 360:
                self.angle = 0
                self.Count = self.Count - 1
            if self.Count > 0:
                Clock.schedule_once(self.set_Circle, 1.0/360)
    

    enter image description here