如何在Kivy中切换小部件的动画过渡

时间:2019-01-20 18:58:12

标签: python animation kivy transition

摘要:

我想测试Kivy的Animation函数的所有可能转换。

在我的代码中,我调用了一种方法来切换按钮的动画。因为我使用print来确认更改,所以该方法被成功调用;但是,按钮的过渡不接受更改。他们继续使用第一个动画,但不使用列表中的下一个动画。

我在做什么错了?

代码:

from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from random import random
from kivy.uix.button import Button
from kivy.animation import Animation
from kivy.clock import Clock


class LittleButtons(Button):

    transition = 'in_back'
    dur = 2
    num = 0

    def change_transition(self):

        list = ['in_back','in_bounce','in_circ','in_cubic','in_elastic','in_expo','in_out_back',
                'in_out_bounce','in_out_cubic','in_out_elastic','in_out_expo',
                'in_out_quad','in_out_quart','in_out_quint','in_out_sine','in_quad','in_quart',
                'in_quint','in_sine','linear','out_back','out_bounce','out_circ','out_cubic',
                'out_elastic','out_expo','out_quad','out_quart','out_quint','out_sine']

        self.num += 1
        self.transition = list[self.num]
        self.reup()
        print(self.transition)

        if self.num == len(list) - 1:
            self.num = -1

    def reup(self, *args):

        Animation.cancel_all(self)
        anim = Animation(pos_hint = {'center_x': random(), 'center_y': random()},
                         duration = self.dur, t = self.transition)
        anim.start(self)

    def __init__(self, **kwargs):

        super(LittleButtons, self).__init__(**kwargs)
        self.pos_hint = {'center_x': random(), 'center_y': random()}
        self.size_hint = None, None
        self.width = random() * (Window.width / 20)
        self.height = random() * (Window.width / 20)
        self.background_color = [0,0,0,.05]

        Animation(pos_hint = {'center_x': random(), 'center_y': random()},
                  duration = self.dur).start(self)
        Clock.schedule_interval(self.reup, self.dur)

KV = Builder.load_string("""
#:import Factory kivy.factory.Factory

Screen:
    FloatLayout:
        on_parent:
            (lambda ltext: [self.add_widget(Factory.LittleButtons(text=ltext)) for i in range (150)])('hi!')
        LittleButtons:
            id: base
        Button:
            background_color: 0,0,0,0
            canvas:
                Color:
                    rgba: 0,1,1,1
                Rectangle:
                    pos: self.pos
                    size:self.size
            on_release:
                base.change_transition()
""")

class MyApp(App):
    def build(self):
        return KV

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

1 个答案:

答案 0 :(得分:1)

您的代码正在运行,但未达到您的期望。您的base.change_transition()调用base,它引用了id的ID。 LittleButtons是您的KV字符串中不是由on_parent事件构建的一个LittleButtons。该LittleButtons的动画过渡已成功修改,但其他过渡不受影响。另外,由于大的Button隐藏了该特定的LittleButtons,所以它是看不见的。因此,您已经成功更改了一个pextrb $3, %xmm0, %eax 的过渡,但未显示。