如何在python kivy中创建动画(每秒更改帧)

时间:2019-10-21 05:50:14

标签: python kivy

我想在python kivy中创建动画(更改图像源),但是我做不到。我尝试使用时钟计划来每0.01秒更改一次图像源,但是它太慢且看起来不流畅。我应该怎么做才能解决它?

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.modalview import ModalView
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
Builder.load_string('''
<RootWidget>
    FloatLayout:
        id: layout
        Image:
            id: background
            size: self.size
            nocache: True
            source: 'background.gif'
        Button:
            id: change_background
            pos:(380, 130)
            on_release: root.change()
            background_normal: 'button.gif'
            size_hint: (None, None)
            size:(60, 60)
''')
class RootWidget(FloatLayout, ModalView):
    def change(self):
        self.num = 0
        self.an = ['an1.gif', 'an2.gif', 'an3.gif']
        Clock.schedule_interval(self.animation, 0.01)

    def animation(self, dt):
        if self.num != 3:
            self.ids.background.source = self.an[self.num]
            self.num += 1
        else:
            self.ids.background.source = 'an4.gif'

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

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

0 个答案:

没有答案