Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

python - How to restart Kivy Animation

I am trying to create a simple countdown timer, i want to be able to restart it on the Button click, right now it won't start other, i just can not get it right. What am i missing here?

the KV

<RootWidget>:
    #:import randint  random.randint
    orientation: "vertical"
    CountDownLbl:
        id: anim_label
        text: "{0:.3f}".format(float(self.startCount - self.angle / 360))
        font_size: 30
        canvas:
            Color:
                rgb: 0,1,0
            Line:
                circle:self.center_x, self.center_y, 90, 0, self.angle % 360
                width: 30
    Button:
        size_hint_y: 0.1
        text: "Start"
        on_press: anim_label.start()

and the code

COUNT=1

class RootWidget(FloatLayout):
    pass

class CountDownLbl(Label):
    startCount = COUNT
    angle = NumericProperty(0)

    def __init__(self, **kwargs):
        super(CountDownLbl, self).__init__(**kwargs)

    def start(self):
        self.startCount = COUNT
        self.anim = Animation(angle=360 * self.startCount,  duration=self.startCount)
        self.in_progress = True
        self.anim.start(self)

class TestApp(App):
    def build(self):
        return RootWidget()

if __name__ == '__main__':
    TestApp().run()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that the first animation animates the angle property to 360, and further animations try to animate the angle property to 360 again, which results in no actual animation. The fix is to increment your COUNT after each animation, like this:

def start(self):
    global COUNT
    self.startCount = COUNT
    self.anim = Animation(angle=360 * self.startCount,  duration=self.startCount)
    self.in_progress = True
    self.anim.start(self)
    COUNT += 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...