【问题标题】:How to get object which is animated in kivy?如何获取在kivy中动画的对象?
【发布时间】:2020-03-19 08:44:38
【问题描述】:

问题:

  • 如何在 Kivy 中获取动画对象?
  • 是否可以获取动画对象?

代码:

from kivy.app import App
from kivy.animation import Animation
from kivy.uix.image import Image
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen


class ScreenOne(Screen):
    def __init__(self, **kwargs):
        super(ScreenOne, self).__init__(**kwargs)
        self.img1 = Image(source="F:\PyCharm Python Works\Kivy Test\opencityicon (1).png")
        box_layout = BoxLayout()
        self.add_widget(box_layout)
        box_layout.add_widget(self.img1)

    def on_enter(self, *args):
        animation = Animation(d=1, x=560)
        animation.start(self.img1)
        print(animation.animated_properties)


sm = ScreenManager()
sm.add_widget(ScreenOne(name='screen_one'))
sm.current = 'screen_one'


class Test(App):
    def build(self):
        return sm


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

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


感谢您的阅读!!

【问题讨论】:

  • 我认为目前没有办法做到这一点。但是你可以继承Animation,并让你的新类保留对动画对象的引用。

标签: python python-3.x kivy


【解决方案1】:

这是AnimationSequenceParallel 类的自定义版本和一个简单的测试用例:

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()

如果您使用它代替Animation,那么您可以访问widgets 动画Widgets 列表。

【讨论】:

  • 这适用于顺序和并行动画吗?
  • 不像我最初发布的那样。顺序和并行动画使用SequenceParallel 类(扩展Animation)。如果我能让他们改为扩展MyAnim,它会起作用。由于我不知道该怎么做,所以我在上面的答案中发布了添加代码,创建了两个新类MySequenceMyParallel。我相信有了这些额外的类,以及MyAdmin 中的两个新的重写方法,您现在可以按照所述访问动画Widget
  • 你能举个例子吗?它不适合我! @约翰安德森
  • 我以两种方式修改了我的答案。我添加了一个简单的测试用例,并删除了stop 方法的覆盖,因此动画小部件列表只能增长。每次 MyAnim(或 MyParallelMySequence)的同一实例用于 start 动画时,动画小部件都会添加到列表中,并且该列表在动画完成后继续可用。
  • 我的意思是说当我尝试使用它时没有属性作为小部件。 @约翰安德森
猜你喜欢
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
相关资源
最近更新 更多