Dmitry
Dmitry

Reputation: 33

Element in BoxLayout kivy

I'm new in kivy and want to create a small app. Which will show different text (loaded from the database). I tried to make a BoxLayout, but it is very difficult, especially since I then have to access the id in this element. I think I took a little wrong turn.

py code

import kivy
from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class Container(BoxLayout):
    def __init__(self, **kwargs):
        super(Container, self).__init__(**kwargs)
        w=BoxLayout()
        self.ids.tr.add_widget(w)
        
class MyApp(App):
    def build(self):
        return Container()


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

kv code

<container>:
    orientation:"vertical"
    Label:
        text: "Plan"
    BoxLayout:
        id:tr
        Label
            id: id1
            text: "text1"
        Label:
            id: id2
            text: "text2"
        Label:
            id: id3
            text: "text3"

I would like something like this: [enter image description here] 1

Upvotes: 0

Views: 43

Answers (1)

Nelzman
Nelzman

Reputation: 26

i hope this answers your question, but basically this creates your app:
app.py:

from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout

class Container(MDBoxLayout):    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ids.id2.text = 'Hello'

#class Container(MDBoxLayout):    
#    pass
        
class MyContainerApp(MDApp):
    def build(self):
        self.container = Container()
        
        return self.container

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

mycontainer.kv:

#:kivy 2.2.1

<container>:
    orientation:"vertical"
    id: container1
    MDLabel:
        text: "Plan"
    MDBoxLayout:
        id: tr1
        MDLabel:
            id: id1
            text: "text1"
        MDLabel:
            id: id2
            text: "text2"
        MDLabel:
            id: id3
            text: "text3"
    MDBoxLayout:
        id: tr2
    MDBoxLayout:
        id: tr3

If you don't need to adress the ids, you could simply initialize the container without further specifications (the commented part in the code).

One additional tip that would have saved me a lot of refactorings some months ago: take a look at the package kivymd, a layer on top of most kivy-classes that enables much nicer visuals for your app.

Upvotes: 0

Related Questions