动画无法像Kivy中的Widget对象一样正常工作 [英] Animation does't work as expected for Widget object in Kivy

查看:94
本文介绍了动画无法像Kivy中的Widget对象一样正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Win7 64上使用Kivy 1.9.0和Python 2.7.
我正在尝试制作一个小部件动画,使其从当前位置移动到特定位置.将Widget元素添加到屏幕后,我启动了动画,但是没有任何反应,我期望Widget会根据Animation位置移动.这是代码:

I am using Kivy 1.9.0 with Python 2.7 on Win7 64.
I am trying to animate a Widget to move from current position to a specific one. After adding the Widget element to the screen I start the animation but nothing happens, I was expecting the Widget to move according to the Animation position. Here is the code:

projectile = Widget(pos=(self._posX + self._projectile_x, self._posY + self._projectile_y))
with projectile.canvas:
    Ellipse(pos=projectile.pos, size=(10,10))

self.add_widget(projectile)

anim = Animation(x=100, y=100)
anim.start(projectile)

椭圆形小窗口小部件不会移动,仅显示在指定的坐标上.如果将Widget对象替换为for.一个Button对象,动画可以正常工作.
您知道为什么会发生这种情况吗?
谢谢

The small Ellipse widget does not move, it just appears on the specified coordinates.If I replace the Widget object with for eg. a Button object the animation works correctly.
Do you have any idea why this happens?
Thanks

推荐答案

因此,据我所知,在动态创建Widget对象(代码方式)时,Kivy不会像对Button或其他对象那样进行相同的绑定.小部件更复杂的类型.因此,解决方案可以是:
1:,如Yoas所说的用kv语言创建对象.的缺点是,它以某种方式迫使您执行以下操作:在kv中保持一致.将它们包含在kv中的好处是kivy为您绑定了所有属性.或
2:自己为属性进行绑定.缺点是您必须盲目地进行绑定,因为您永远不知道在不同情况下使用的属性是什么.

在我的特殊情况下,我假定仅pos属性被更改,因为我使用了Animation对象,仅更改小部件的位置.为了进行绑定,我为Widget创建了一个类,以便可以更清晰地对其进行管理.
之后:

So, from what I can understand, when creating a Widget object dinamically(codewise), Kivy doesn't do the same bindings as it does for Button or other Widget more complex types. So, a solution can be:
1: as Yoas said to create the objects in kv language. This comes with the drawback that somehow it forces you to do the rest in kv to be consistent. The plus of having them in kv is that kivy binds all properties for you. OR
2: do the bindings for the properties yourself. The drawback is that you must do blindly the bindings because you never know what are those properties that are used in different situations.

In my particular case I assumed that only pos property is changed because I used an Animation object that only changes the position of the Widget. To do the binding I created a class for the Widget so I can manage it more clearly.
AFTER:

class Projectile(Widget):

def __init__(self, pos, size):
    super(Projectile, self).__init__(pos=pos, size=size)
    with self.canvas:
        self.ellipse = Ellipse(pos=pos, size=size)

    self.bind(pos = self.updatePosition)

def updatePosition(self, *args):
    self.ellipse.pos = self.pos

然后我使用了Projectile对象而不是默认的Widget:

And then I used Projectile object instead of my default Widget:

proj = Projectile(pos=(self._posX + self._projectile_x, self._posY + self._projectile_y), size = (10, 10))

self.add_widget(proj)

animation = Animation(pos=(100, 100))
animation.start(proj)

现在可以正常工作了,请向Yoav提示

This works fine now, thx Yoav for the hint

这篇关于动画无法像Kivy中的Widget对象一样正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆