Python 如何获取在kivy中设置动画的对象? 问题: 如何获取在Kivy中设置动画的对象 有可能得到动画对象吗 代码:

Python 如何获取在kivy中设置动画的对象? 问题: 如何获取在Kivy中设置动画的对象 有可能得到动画对象吗 代码:,python,python-3.x,kivy,Python,Python 3.x,Kivy,我无法在Kivy的文档中找到我问题的答案! 谢谢你的阅读 下面是动画、序列和并行类的定制版本以及一个简单的测试用例: from kivy.animation import Animation, Sequence, Parallel from kivy.app import App from kivy.clock import Clock from kivy.properties import ListProperty from kivy.uix.button import Button fro

我无法在Kivy的文档中找到我问题的答案!


谢谢你的阅读 下面是
动画
序列
并行
类的定制版本以及一个简单的测试用例:

from kivy.animation import Animation, Sequence, Parallel
from kivy.app import App
from kivy.clock import Clock
from kivy.properties import ListProperty
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label


class MySequence(Sequence):
    widgets = ListProperty([])

    def start(self, widget):
        super(MySequence, self).start(widget)
        self.widgets.append(widget)

    def __add__(self, animation):
        return MySequence(self, animation)

    def __and__(self, animation):
        return MyParallel(self, animation)

class MyParallel(Parallel):
    widgets = ListProperty([])

    def start(self, widget):
        super(MyParallel, self).start(widget)
        self.widgets.append(widget)

    def __add__(self, animation):
        return MySequence(self, animation)

    def __and__(self, animation):
        return MyParallel(self, animation)

class MyAnim(Animation):
    widgets = ListProperty([])

    def start(self, widget):
        super(MyAnim, self).start(widget)
        self.widgets.append(widget)

    def __add__(self, animation):
        return MySequence(self, animation)

    def __and__(self, animation):
        return MyParallel(self, animation)


class TestApp(App):
    def animate(self, instance):
        self.animation = MyAnim(pos=(200, 200), d=5)
        self.animation.on_complete = self.completed

        # sequential
        # self.animation += MyAnim(pos=(400, 400), t='out_sine')

        # parallel
        self.animation &= MyAnim(size=(200, 300), d=5)

        Clock.schedule_once(self.another_anim, 1)

        self.animation.start(instance)

    def another_anim(self, dt):
        self.animation.start(self.label)

    def completed(self, widget):
        print('Animation completed - animated Widget:', widget)
        Clock.schedule_once(self.check_anim, 2)

    def check_anim(self, dt):
        print(dt, 'seconds after Animation completed - animated Widgets:', self.animation.widgets)

    def build(self):
        fl = FloatLayout()
        button = Button(size_hint=(None, None), size=(100,50), pos=(0,0), text='click here', on_press=self.animate)
        fl.add_widget(button)
        self.label = Label(text='label', size_hint=(None, None), size=(100, 500), pos=(400, 200))
        fl.add_widget(self.label)
        return fl

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

如果您使用它来代替
动画
,那么您可以访问
小部件
列表中的动画
小部件

我认为目前没有办法做到这一点。但是您可以将
动画
子类化,并让您的新类保留对动画对象的引用。这是否适用于顺序和并行动画?不像我最初发布的那样。顺序和并行动画使用
顺序
并行
类(扩展
动画
)。如果我能让他们扩展
MyAnim
,它就会起作用。因为我不知道怎么做,我在上面的答案中添加了代码,创建了两个新类
MySequence
MyParallel
。我相信有了这些额外的类,以及
MyAdmin
中两个新的重写方法,您现在可以访问所描述的动画
小部件了。您可以发布一个示例吗?这对我没用@约翰·安德森从两个方面修改了我的答案。我添加了一个简单的测试用例,并且删除了对
stop
方法的覆盖,因此动画小部件的列表只能增加。每次使用
MyAnim
(或
MyParallel
MySequence
)的同一个实例来
启动
动画时,动画小部件会添加到列表中,并且该列表在动画完成后仍然可用。我的意思是,当我试图使用它时,它说没有属性作为小部件@约翰·安德森
from kivy.animation import Animation, Sequence, Parallel
from kivy.app import App
from kivy.clock import Clock
from kivy.properties import ListProperty
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label


class MySequence(Sequence):
    widgets = ListProperty([])

    def start(self, widget):
        super(MySequence, self).start(widget)
        self.widgets.append(widget)

    def __add__(self, animation):
        return MySequence(self, animation)

    def __and__(self, animation):
        return MyParallel(self, animation)

class MyParallel(Parallel):
    widgets = ListProperty([])

    def start(self, widget):
        super(MyParallel, self).start(widget)
        self.widgets.append(widget)

    def __add__(self, animation):
        return MySequence(self, animation)

    def __and__(self, animation):
        return MyParallel(self, animation)

class MyAnim(Animation):
    widgets = ListProperty([])

    def start(self, widget):
        super(MyAnim, self).start(widget)
        self.widgets.append(widget)

    def __add__(self, animation):
        return MySequence(self, animation)

    def __and__(self, animation):
        return MyParallel(self, animation)


class TestApp(App):
    def animate(self, instance):
        self.animation = MyAnim(pos=(200, 200), d=5)
        self.animation.on_complete = self.completed

        # sequential
        # self.animation += MyAnim(pos=(400, 400), t='out_sine')

        # parallel
        self.animation &= MyAnim(size=(200, 300), d=5)

        Clock.schedule_once(self.another_anim, 1)

        self.animation.start(instance)

    def another_anim(self, dt):
        self.animation.start(self.label)

    def completed(self, widget):
        print('Animation completed - animated Widget:', widget)
        Clock.schedule_once(self.check_anim, 2)

    def check_anim(self, dt):
        print(dt, 'seconds after Animation completed - animated Widgets:', self.animation.widgets)

    def build(self):
        fl = FloatLayout()
        button = Button(size_hint=(None, None), size=(100,50), pos=(0,0), text='click here', on_press=self.animate)
        fl.add_widget(button)
        self.label = Label(text='label', size_hint=(None, None), size=(100, 500), pos=(400, 200))
        fl.add_widget(self.label)
        return fl

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