Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation does't work as expected for Widget object in Kivy

Tags:

python

kivy

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)

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

like image 999
Lucian Avatar asked Oct 15 '25 04:10

Lucian


2 Answers

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

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)

This works fine now, thx Yoav for the hint

like image 170
Lucian Avatar answered Oct 17 '25 19:10

Lucian


When you do it in python the Ellipse.pos is not binded to the projectile.pos

If you would do the same in kv lang it will work fine

like image 44
Yoav Glazner Avatar answered Oct 17 '25 18:10

Yoav Glazner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!