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:
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'
from kivy.app import Appfrom kivy.uix.widget import Widgetfrom kivy.uix.boxlayout import BoxLayoutfrom kivy.uix.image import Imagefrom kivy.uix.togglebutton import ToggleButtonfrom 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()