Python Kivy小部件动画

问题描述:

我最近发布了类似的话题,但是这次我会尽量做得更清楚明确。我的问题是,基维的小部件没有像预期的那样动画。下面是一些示例代码,为什么散射更好的动画比小部件:Python 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()函数都不起作用。有没有解决方法?

好的,我解决了这个问题。我在子构件类中使用了on_touch_move()方法,解决方案是在父构件中使用它。那么分散选择就没有问题。