Python Kivy小部件动画

时间:2015-02-08 16:27:50

标签: python animation widget kivy scatter

我最近发布了类似的话题,但这次我会尝试更加明确和具体。我的问题是,Kivy中的小部件并不像预期的那样动画。这是一些示例代码,为什么散布比小部件更好地动画:

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.scatter import Scatter
from kivy.animation import Animation
from kivy.graphics import Color, Rectangle

class ExampleWidget(Widget):
    def __init__(self, **kwargs):
        super(ExampleWidget, self).__init__(**kwargs)
        self.size = (100,100)
        self.pos = (100,100)
        with self.canvas:
            Color(1,0,0)
            self.texture = Rectangle(size=self.size, pos=self.pos)

    def on_pos(self, obj, value):
        try: self.texture.pos = value
        except: pass

class ExampleScatterTexture(Widget):
    def __init__(self, **kwargs):
        super(ExampleScatterTexture, self).__init__(**kwargs)
        with self.canvas:
            Color(0,1,0)
            texture = Rectangle(size=self.size, pos=self.pos)

class ExampleScatter(Scatter):
    def __init__(self, **kwargs):
        super(ExampleScatter, self).__init__(**kwargs)
        self.do_rotation = False
        self.do_scale = False
        self.do_translation = False
        self.size = (100,100)
        self.pos = (100,300)
        texture = ExampleScatterTexture(size=self.size)
        self.add_widget(texture)

class ExampleScreen(Widget):
    def __init__(self, **kwargs):
        super(ExampleScreen, self).__init__(**kwargs)
        self.size = Window.size

        example_widget = ExampleWidget()
        self.add_widget(example_widget)

        example_scatter = ExampleScatter()
        self.add_widget(example_scatter)

        #SCATTER IS GREEN, WIDGET IS RED
        example_widget_animation = Animation(pos=(300,100), duration=2., t='in_bounce') + Animation(pos=(100,100), duration=2., t='in_bounce')
        example_scatter_animation = Animation(pos=(300,300), duration=2., t='in_bounce') + Animation(pos=(100,300), duration=2., t='in_bounce')
        example_widget_animation.repeat = True
        example_scatter_animation.repeat = True
        example_widget_animation.start(example_widget)
        example_scatter_animation.start(example_scatter)

class BadAnimationExample(App):
    def build(self):
        root = ExampleScreen()
        return root

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

正如您所看到的,窗口小部件动画执行得非常快,然后会出现暂停,而分散动画非常像我们期望的那样。我的问题是当我把手指放在散点图上时,我的所有on_touch_move()函数都不起作用。有没有解决方案?

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题。我在子窗口小部件类中使用了我的on_touch_move()方法,解决方案是在父窗口小部件中使用它。然后散射选择没有问题。