【问题标题】:Removing a widget with Kivy使用 Kivy 删除小部件
【发布时间】:2018-04-27 06:31:48
【问题描述】:

我正在尝试在动画完成后删除 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)

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    您不必使用画布添加动画,而是直接使用add_widget() 添加小部件,然后使用remove_widget() 将其删除。在您最初的情况下,bullet1 不是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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      相关资源
      最近更新 更多