如何在kivy中更新属性时启动动画?

时间:2014-01-09 12:01:58

标签: kivy

这是我的代码:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import NumericProperty


Builder.load_string('''
<Simple>:
    Label:
        text: str( root.sometext )
    Button:
        text: '+++'
        on_release: root.inc()
''')


class Simple(BoxLayout):
    sometext = NumericProperty(0)

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

        self.sometext = 5

    def inc(self):
        self.sometext += 5 


class TApp(App):
    def build(self):
        return Simple()


TApp().run()

一切正常,每次按下按钮标签都会更新5。

我不想添加的是一些动画。 例如:更新号码前往左侧,更新后的号码来自右侧。 如果这是动画复杂的,那么欢迎其他概念。

怎么做? 我查看了文档,但每个示例都是postion,而不是文本更新(或至少我发现的内容)。

2 个答案:

答案 0 :(得分:4)

@inclement是对的。我不认为可以直接为文本设置动画,但是可以为Label设置动画。也可以连接动画(operator '+')并使用动画的事件on_complete将事物放在中间,就像你正在寻找的增量一样。

这很简单,但您需要对代码进行一些更改:

  1. 您需要能够从Python访问Label

    1.1。在标签中添加id

    Label:
        id: _the_label
    

    1.2。将ObjectProperty添加到Simple类:

    class Simple(BoxLayout):
        the_label = ObjectProperty(None)
    

    1.3。 “连接”idObjectProperty

    <Simple>:
       the_label: _the_label
    
  2. 确保Label有空间向左或向右移动。一种方法:

    2.1将Label嵌入另一个Widget,在本例中为RelativeLayout

    RelativeLayout:
       Label:
    

    2.2定义Label的大小并将其居中:

    size_hint: 0.3, 0.1
    center_x: self.parent.width/2
    center_y: self.parent.height/2
    
  3. 现在,您可以继续创建动画Label

    的方法
    def animate(self):
        left = Animation(x=0, color= [0,0,0,0])
        left.bind(on_complete=self.inc)
        right = Animation(center_x=self.the_label.parent.width/2, color= [1,1,1,1])
        anim = left + right
        anim.start(self.the_label)
    
    def inc(self, instance, value):
        self.sometext += 5 
    

    注意:方法公司bind左侧动画的on_complete。另请注意anim = left + right以连接两个动画。还有运算符*来运行并行动画。

  4. 尽管无法直接为文本设置动画,但Label的某些属性会间接影响它。例如,font_size,color等。这是complete list。我很肯定,如果你真的需要为文本设置动画,应该有办法通过padding property的动画来破解文本的移动。

    最终的代码在这里:

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.lang import Builder
    from kivy.properties import NumericProperty, ObjectProperty
    from kivy.animation import Animation
    
    Builder.load_string('''
    <Simple>:
        the_label: _the_label
        RelativeLayout:
            Label:
                id: _the_label
                size_hint: 0.3, 0.1
                center_x: self.parent.width/2
                center_y: self.parent.height/2
                text: str( root.sometext )
        Button:
            text: '+++'
            on_release: root.animate()
    ''')
    
    
    class Simple(BoxLayout):
        the_label = ObjectProperty(None)
        sometext = NumericProperty(5)
    
        def animate(self):
            left = Animation(x=0)
            left.bind(on_complete=self.inc)
            right = Animation(center_x=self.the_label.parent.width/2)
            anim = left + right
            anim.start(self.the_label)
    
        def inc(self, instance, value):
            self.sometext += 5 
    
    class TApp(App):
        def build(self):
            return Simple()
    
    TApp().run()
    

答案 1 :(得分:1)

  

怎么做?我查看了文档,但每个示例都是postion,而不是文本更新(或至少我发现的内容)。

Kivy的动画修改了特定的属性,我不认为动画文本在这种情况下实际上是有意义的。

听起来想要得到你想要的行为你真的需要添加一个或多个其位置被动画化以获得移动效果的标签 - 也就是说,Animation类不允许你为一个属性(文本)设置动画通过改变他人(pos),它只涉及以一种特定的方式改变一个特定的财产。

  

如果动画很复杂,欢迎使用其他概念。

如果我想这样做,我实际上会看到使用带有两个标签的Carousel小部件。 inc方法将更改当前隐藏标签的文本,然后调用切换当前可见小部件的Carousel方法。 Carousel已经有了一个动画,其中第一个小部件在下一个小部件滑落时滑落,因此它将为您处理细节。

这是非常具体的,使用Carousel恰好适合您的特定问题。我认为你问题的真正答案是你需要考虑自己添加更复杂的行为,因为它不仅仅是动画一个属性的情况。