G.N.
G.N.

Reputation: 139

Removing a widget with Kivy

I'm trying to remove an Image widget after its animation is complete. So far I have managed to animate the widget and then call the animation_complete method after the animation ends. Unfortunately, the widget is not removed.

What am I doing wrong?

class ShootButton(Widget):
    def bullet_fly(self):
        def animation_complete(animation, widget):
            print "removing animation"
            self.remove_widget(widget=bullet1)


        with self.canvas:
            bullet1 = Image(source='bullet.png', pos = (100,200))
            animation1 = Animation(pos=(200, 300))
            animation1.start(bullet1)
            animation1.bind(on_complete=animation_complete)

Upvotes: 2

Views: 870

Answers (1)

eyllanesc
eyllanesc

Reputation: 244369

You do not have to use the canvas to add an animation but add the widget directly using add_widget() and then remove it with remove_widget(). In your initial case bullet1 is not the children of ShootButton.

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.animation import Animation


Window.size = (360, 640)

class ShootButton(Widget):
    def bullet_fly(self):
        def animation_complete(animation, widget):
            self.remove_widget(widget)
        bullet1 = Image(source='bullet.png', pos = (100,200))
        self.add_widget(bullet1)
        animation1 = Animation(pos=(200, 300))
        animation1.start(bullet1)
        animation1.bind(on_complete=animation_complete)


class MyApp(App):
    def build(self):
        button = ShootButton()
        button.bullet_fly()
        return button


if __name__ == '__main__':
    MyApp().run()

Upvotes: 3

Related Questions