Python Kivy动画类用于设置Kivy图像的动画

Python Kivy动画类用于设置Kivy图像的动画,python,animation,kivy,Python,Animation,Kivy,Python 3.4 基维1.10.0 我正在尝试使用Kivy动画类来制作我的Animage类的动画。这是因为我想分别修改每个图像的动画延迟和位置值 我想修改图像类的“source”属性。虽然它可以识别“source”属性,但它会尝试将文件名字符串文件相乘 如何修改图像文件名 工作示例: from kivy.app import App from kivy.animation import Animation from kivy.clock import Clock from kivy.uix.

Python 3.4 基维1.10.0

我正在尝试使用Kivy动画类来制作我的Animage类的动画。这是因为我想分别修改每个图像的动画延迟和位置值

我想修改图像类的“source”属性。虽然它可以识别“source”属性,但它会尝试将文件名字符串文件相乘

如何修改图像文件名

工作示例:

from kivy.app import App
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.image import Image

class Test(App):
    def __init__(self, **kwargs):
        super(Test, self).__init__(**kwargs)

        self.content = BoxLayout()
        self.content.orientation = 'vertical' 
        self.anim_image = Image(source = 'image1.png')
        self.anim_button = Button(on_press=self.animate_image)
        self.content.add_widget(self.anim_image)
        self.content.add_widget(self.anim_button)

    def build(self):
        return self.content

    def animate_image(self, *args, **kwargs):
        image_animate = Animation()
        for i in range(4):
            image_animate += Animation(x = (i + 10), source = 'image' + str(i) + '.png', duration=(0.10 * i))
        image_animate.start(self.anim_image)


if __name__ == '__main__':
    Test().run()
错误日志:

[INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "C:\Python34x86\projects\rogue_like_app\repeatable issues\animation_image_source.py", line 31, in <module>
     Test().run()
   File "C:\Python34x86\lib\site-packages\kivy\app.py", line 828, in run
     runTouchApp()
   File "C:\Python34x86\lib\site-packages\kivy\base.py", line 504, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Python34x86\lib\site-packages\kivy\core\window\window_sdl2.py", line 663, in mainloop
     self._mainloop()
   File "C:\Python34x86\lib\site-packages\kivy\core\window\window_sdl2.py", line 405, in _mainloop
     EventLoop.idle()
   File "C:\Python34x86\lib\site-packages\kivy\base.py", line 339, in idle
     Clock.tick()
   File "C:\Python34x86\lib\site-packages\kivy\clock.py", line 581, in tick
     self._process_events()
   File "kivy\_clock.pyx", line 367, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7700)
   File "kivy\_clock.pyx", line 397, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7577)
   File "kivy\_clock.pyx", line 395, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7498)
   File "kivy\_clock.pyx", line 167, in kivy._clock.ClockEvent.tick (kivy\_clock.c:3490)
   File "C:\Python34x86\lib\site-packages\kivy\animation.py", line 345, in _update
     value = calculate(a, b, t)
   File "C:\Python34x86\lib\site-packages\kivy\animation.py", line 376, in _calculate
     return (a * (1. - t)) + (b * t)
 TypeError: can't multiply sequence by non-int of type 'float'

这是不是意味着我要做的动画不是Kivy代码中固有的?或者,如何解决此问题以使动画正确循环?

发生此错误是因为动画无法从str对象转换为str对象“image0.png”->“image1.png”

解决此问题的一种方法是使用完整回调上的

def animate_image(self, *args, **kwargs):
    image_animate = Animation()

    def f(i, w):
        w.source = 'image%d.jpg' % i


    for i in range(4):

        a = Animation(x = (i + 10), duration=(0.10 * i),
                      )
        a.on_complete = functools.partial(f, i)


        image_animate += a

    image_animate.start(self.anim_image)
另一个选项是在图像上有一个image_num属性,并将其绑定,以便相应地更改图像

例如,在KV文件中:

<MyImage>:
    image_num: 0
    source: "image%d.png" % int(self.image_num)

谢谢你,这确实奏效了,并且已经接受了它作为一种解决方案。但是,我无法使用它,因为在使用它时似乎有半秒的延迟。我不认为有任何理由会有很大的延迟,也许你的图像非常大?
<MyImage>:
    image_num: 0
    source: "image%d.png" % int(self.image_num)
image_animate += Animation(x = (i + 10), image_num=i, duration=(0.10 * i))