2017-07-06 81 views
1

的Python 3.4 Kivy 1.10.0Kivy動漫類動畫Kivy圖片

我試圖使用Kivy動畫類動畫我的Image類。這是因爲我想單獨修改每個圖像的anim_delay和位置值。

我想修改圖像類的'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代碼animation.py我看到從上方返回一個錯誤的值是:

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

這是否意味着這樣做的動畫就像我打算在基維代碼中不固有的?或者,我該如何解決此問題,以便動畫能夠正確循環?

回答

1

錯誤發生的原因動畫不能做到從STR對象過渡到STR對象「image0.png」 - > 「image1.png」!?要解決此

的一種方法是使用on_complete回調

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) 

,並在.py文件:

image_animate += Animation(x = (i + 10), image_num=i, duration=(0.10 * i)) 
+0

謝謝這確實工作,並接受它作爲一個解決方案。但是,我將無法使用它,因爲使用它時似乎有半秒的延遲。 –

+0

我看不到任何延遲很大的原因,也許你的圖像非常大? –