Cannot hide widget in box layout

615 views
Skip to first unread message

M Dammer

unread,
May 8, 2017, 1:09:08 PM5/8/17
to Kivy users support


My application consists of an image with a widget bar to the left and to the right. I want to be able to hide these bars when a toggle button is pressed.

I have created a callback that toggles the size_hint_x property of both bars (BoxLayouts). While the right bar disappears and the image stretches to the right as they should, the left bar stays untouched (see second image at the bottom).


Here are the KV and Python files:

<MyScreen>:
    image_wid: my_image
    osd_wid: my_osd
    lpan_wid: my_lpan
    rpan_wid: my_rpan
    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            BoxLayout:
                id: my_lpan
                size_hint_x: 0.1
                Button:
                    text: 'left'
            Image:
                id: my_image
                source: '1.png'
                allow_stretch: True
            BoxLayout:
                id: my_rpan
                size_hint_x: 0.1
                Button:
                    text: 'right'
        BoxLayout:
            size_hint_y: 0.1
            ToggleButton:
                id: my_osd
                state: 'down'
                text: 'Show / Hide bars'


#!/usr/bin/env python
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.togglebutton import ToggleButton
from kivy.properties import ObjectProperty


class MyScreen(BoxLayout):
    osd_wid = ObjectProperty()
    lpan_wid = ObjectProperty()
    rpan_wid = ObjectProperty()


class BartestApp(App):

    def osd_callback(self, instance, value):
        if value == 'normal':
            self.rootwidget.lpan_wid.size_hint_x = 0
            self.rootwidget.rpan_wid.size_hint_x = 0
        else:
            self.rootwidget.lpan_wid.size_hint_x = 0.1
            self.rootwidget.rpan_wid.size_hint_x = 0.1

    def build(self):
        self.rootwidget = MyScreen()
        self.rootwidget.osd_wid.bind(state=self.osd_callback)
        return self.rootwidget


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






1.png
bartest.kv
bartest.py

ZenCODE

unread,
May 10, 2017, 9:35:39 AM5/10/17
to Kivy users support
That does seem like a bug. I've played around a bit but the button always has a width, even when you disable the lpan and rpan size hints and manually set the widths to 0.

Please log an issue posting you code with it.

In the meantime, you can achieve the same using a floatlayout (moving them off screen) or setting their opacity to 0 and disabling the button?

Thanks

M Dammer

unread,
May 10, 2017, 9:52:07 AM5/10/17
to Kivy users support
Thanks for your help!
I could achieve a partial solution of the problem by using a GridLayout with three columns as the Parent, but this brought up the problem that when I set size_hint_x = 0 for the sidebars they are only almost disappearing, but there is always a bit of widget left.
I think a generic boolean property that allows to completely make a widget non-existent in the UI and that makes all these kludges history is overdue in Kivy.

ZenCODE

unread,
May 10, 2017, 1:29:00 PM5/10/17
to Kivy users support
That is predicable based on the BoxLayout behavior. I really suggest using a FloatLayout and just moving them off-screen. One benefit of this approach is that you can animate using the Animation object. So they can slide out, in a fluid and sexy way.

That's one of the benefits of Kivy's novel interface. It's can be more that just a Material design/iOS interface guideline. It can be beautiful and interactive :-)

ZenCODE

unread,
May 10, 2017, 1:29:44 PM5/10/17
to Kivy users support
ps. If you're unclear on how to do this, please ask and I will post an example....

M Dammer

unread,
May 10, 2017, 1:32:43 PM5/10/17
to Kivy users support
Thanks, I Have done this now and everything works as expected.
Reply all
Reply to author
Forward
0 new messages