用Kivy删除小部件

2024-04-19 01:19:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在动画完成后删除Image小部件。 到目前为止,我已经成功地制作了小部件的动画,然后在动画结束后调用animation_complete方法。不幸的是,这个小部件没有被移除。在

我做错什么了?在

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)

Tags: 方法posimageself部件def动画widget
1条回答
网友
1楼 · 发布于 2024-04-19 01:19:21

您不必使用画布添加动画,而是直接使用add_widget()添加小部件,然后使用remove_widget()将其移除。在初始情况下,bullet1不是{}的子代。在

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()

相关问题 更多 >