Search code examples
pythonkivykivy-language

how to use nested for loop with on_parent in kivy


i want to make nested for loop in kivy language by using on_perent then i try the slashes but there is error invalid indentation must be a multiple of 4 spaces i don't know what to do and there is no other than slashes

from kivy.app import App
from kivy.lang import Builder

kv = Builder.load_string('''
#:import Button kivy.uix.button.Button
BoxLayout:
    BoxLayout:
        on_parent:for i in range(2):\n \t \b\b\b\b\b \
        on_parent:for i in range(3):\n \t \
        on_parent:self.add_widget(Button(text=str(i)))
''')

class TestApp(App):
    def build(self):
        return kv

TestApp().run()

Solution

  • This is a way of doing the nested for loop but I don't understand what you are trying to achieve

    from kivy.app import App
    from kivy.lang import Builder
    
    kv = Builder.load_string('''
    #:import Button kivy.uix.button.Button
    BoxLayout:
            BoxLayout:
                    on_parent:
                            if not self.children: [self.add_widget(Button(text=str(j))) for i in range(2) for j in range(3)]
    ''')
    
    class TestApp(App):
        def build(self):
            return kv
    
    TestApp().run()