How to add the same Layout to Layout via kv file?

263 views Asked by At

There is a problem that it is impossible to add BoxLayout to BoxLayout via kv file.

GTN.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button


class GTNApp(App):
    def build(self):
        return Main()

class Main(BoxLayout):
    pass

if __name__ == "__main__":
    GTNApp().run()

GTN.kv

<BoxLayout>:
    orientation: 'vertical'
    BoxLayout:
        orientation: 'horizontal'
        Button:
            text: '1'

После этого выдает ошибку: AssertionError

2

There are 2 answers

1
Ali Shazin On

Python File. GTN.py

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


    class GTNApp(App):
        def build(self):
            self.screen = Builder.load_file('GTN.kv')
            return self.screen

    if __name__ == "__main__":
        GTNApp().run()

Kv File . GTN.kv

   BoxLayout:
       orientation: 'vertical'
       BoxLayout:
           orientation: 'horizontal'
           Button:
               text: '1'

    
0
Maksim Ivanov On

Solved my problem like this.

GTN.py

from kivy.app import App
from kivy.uix.widget import Widget

class Main(Widget):
    pass

class GTNApp(App):
    def build(self):
        return Main()

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

GTN.kv

<Main>
    BoxLayout:
        orientation: 'vertical'
        size: root.width, root.height
        BoxLayout:
            orientation: 'horizontal'
            Button:
                text: '1'