Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kivy Animation Class to Animate Kivy Images

Python 3.4 Kivy 1.10.0

I am attempting to use Kivy Animation class to animate my an Image class. This is so because I want to modify the anim_delay and position values for each image separately.

I want to modify the 'source' property of an image class. Although it recognizes the 'source' property, it attempts to multiply the filename string file.

How can I modify the image file names?

Working Example:

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

Error Log:

[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'

Looking into animation.py from Kivy Code I see that the values that return an error from above are:

a = image2.png
b = image1.png
t = 0.0

Does this mean this doing an animation like I was planning to in not inherent in the Kivy code? Or, how do I fix this issue so that the animation will loop correctly?

like image 960
Kevin Alvarez Avatar asked Mar 08 '23 16:03

Kevin Alvarez


1 Answers

The error occurs because the Animation cannot do a transition from an str object to an str object "image0.png" -> "image1.png" !?

One way to work around this is to use the on_complete callback

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)

Another option is to have a image_num property on the image and bind it so it will change the image accordingly ...

for example in the KV file:

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

and in the .py file:

image_animate += Animation(x = (i + 10), image_num=i, duration=(0.10 * i))
like image 89
Yoav Glazner Avatar answered Mar 18 '23 00:03

Yoav Glazner