How to hide an ActionButton in Kivy?

前端 未结 4 941
醉梦人生
醉梦人生 2020-12-21 04:04

I am trying to modify the visibility of an ActionButton accordingly to the current screen (using Screen Manager). I could not find a Visible property or something like that

相关标签:
4条回答
  • 2020-12-21 04:38

    Removing widgets from parents is not a very good idea, if the goal is to hide them. When something is removed from the Kivy tree structure, it will be picked up by the garbage collector. You could try to keep a reference to it, but conceptually hidding is different from removing.

    The best solution for me has been using the opacity property. It is kind of the same principle of a visible property but more powerful because it accepts gradients (and animations).

    The "caveat" is that you have to consider that the Widget is still there. It is just invisible. In some cases, you might want to try and combine the opacity with the disabled property.

    Button:
        opacity: 0
        disabled: True # To make sure it doesn't capture events accidentally
    
    0 讨论(0)
  • 2020-12-21 04:42

    You could try to set size_hint to 0. I did this to hide a layout that was nested inside a BoxLayout. It worked flawlessly. Another method is to remove the widget from its parent. But if you are planning to re-add it you need to instantiate it again. For example:

    widget = Widget()
    parent.add_widget(widget)
    parent.remove_widget(widget)
    # Create the widget again
    widget = Widget()
    parent.add_widget(widget)
    
    0 讨论(0)
  • 2020-12-21 05:00

    I'm still learning kivy but it appears that, surprisingly, there is no property or member function to do this. Instead, you have to remove the widget from its parent, or set its color alpha to 0 (which will only work in cases where you have one color).

    Update:

    The traceback indicates that self.next still has a parent when self.ids.av.add_widget(self.next) is called. Since this call is preceded by a self.ids.av.clear_widgets(), the only way that self.next is still in widget tree is that it is actually not a child of self.ids.av. Maybe it is a child of the default layout used by av, and layout doesn't immediately get garbage collected. Try

    print 'next in av.children:', self.next in self.ids.av.children
    print 'parent of next:', self.next.parent
    # self.ids.av.clear_widgets()
    # self.ids.av.add_widget(self.next)
    parent = self.next.parent
    parent.remove_widget(self.next)
    parent.add_widget(self.next)
    
    0 讨论(0)
  • 2020-12-21 05:04

    There's a better way to hide widgets than adding or removing them. Simply set the position to include an offscreen coordinate :

    # save the old y-coordinate of MyWidget.pos
    root.saved_y = MyWidget.y
    # Now move the widget offscreen (recall that pos is just an alias for (x, y))
    MyWidget.y = 5000
    

    (I've tested this solution; it works.)

    0 讨论(0)
提交回复
热议问题