Would an approach similar to the example below be possible in Kivy?
The code posted obviously doesn't work, and again it's only an example: I will need different layouts to be drawn depending on a certain property.
How would you suggest working this out?
BoxLayout:number: 0if self.number > 3:Label:text: 'number is bigger than 3'Button:text: 'click here to decrease'on_press: root.number -= 1else:Label:text: 'number is smaller than 3'Button:text: 'click here to increase'on_press: root.number += 1
KV lang has only a limited functionality, so if you want more control you should put your logic in the Python code. For example you can move your layouts into separate widgets and then dynamically select proper one from Python code with add_widget()
and remove_widget()
.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty
from kivy.lang import BuilderBuilder.load_string('''
<SubWidget1>:Label:text: 'number is bigger than 3'Button:text: 'click here to decrease'on_press: root.parent.number -= 1<SubWidget2>:Label:text: 'number is smaller than 3'Button:text: 'click here to increase'on_press: root.parent.number += 1<MyWidget>number: 0
''')class SubWidget1(BoxLayout):passclass SubWidget2(BoxLayout):passclass MyWidget(BoxLayout):number = NumericProperty()def __init__(self, *args):super(MyWidget, self).__init__(*args)self.widget = Noneself._create_widget()def _create_widget(self):print(self.number)if self.widget is not None:self.remove_widget(self.widget)if self.number > 3:self.widget = SubWidget1()else:self.widget = SubWidget2()self.add_widget(self.widget)def on_number(self, obj, value):self._create_widget()class MyApp(App):def build(self):return MyWidget()if __name__ == '__main__':MyApp().run()