gpt4 book ai didi

python - Kivy 动画类动画 Kivy 图像

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:45 27 4
gpt4 key购买 nike

python 3.4基维 1.10.0

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

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

如何修改图片文件名?

工作示例:

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

这是否意味着像我计划的那样制作动画并不是 Kivy 代码固有的?或者,如何解决此问题以便动画正确循环播放?

最佳答案

错误发生是因为Animation 无法从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 属性并绑定(bind)它,这样它就会相应地改变图像......

例如在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))

关于python - Kivy 动画类动画 Kivy 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44955913/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com