Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add multiple labels in .kv file

My problem is that I want to add multiple labels without having to repeat so many lines of code. I have searched for solutions for a long time and all I see is simply writing a for loop in the python file instead of working on the .kv file. However, the location of the labels I want to add is inside a GridLayout inside a scrollLayout inside a BoxLayout and inside another BoxLayout. Is the only solution really to code all of that in my python file? Is there a better approach to this solution?

This is my first time asking a question on StackOverflow, I am very new to all of this, please correct me if I haven’t asked the question in a conventional or clear format. Thank you very much.

python code

from kivy.uix.widget import Widget
from kivy.app import App
from kivy.lang import Builder
Builder.load_file('widgetq.kv')



class Win(Widget):
    pass



class WidgetApp(App):
    def build(self):
        return Win()


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

.kv file code

<Win>
    box1:box1
    BoxLayout:
        size: root.size
        orientation: "vertical"
        BoxLayout:
            size_hint: 1, 5
            ScrollView:
                GridLayout:
                    id:box1
                    orientation: 'tb-lr'
                    height: self.minimum_height
                    size_hint_y: None
                    row_default_height:180
                    spacing: 2
                    cols:1
                    Label:
                        background_color:(150/255, 150/255, 150/255, 1)
                        text:"table"
                        canvas.before:
                            Color:
                                rgba: self.background_color
                            Rectangle:
                                size: self.size
                                pos: self.pos
                    Label:
                        background_color:(150/255, 150/255, 150/255, 1)
                        text:"table"
                        canvas.before:
                            Color:
                                rgba: self.background_color
                            Rectangle:
                                size: self.size
                                pos: self.pos

                    Label:
                        background_color:(150/255, 150/255, 150/255, 1)
                        text:"table"
                        canvas.before:
                            Color:
                                rgba: self.background_color
                            Rectangle:
                                size: self.size
                                pos: self.pos

                    Label:
                        background_color:(150/255, 150/255, 150/255, 1)
                        text:"table"
                        canvas.before:
                            Color:
                                rgba: self.background_color
                            Rectangle:
                                size: self.size
                                pos: self.pos



        BoxLayout:
            size_hint: 1, 1
            Label:
                background_color:(94/255, 94/255, 94/255, 1)
                text:"tab"
                canvas.before:
                    Color:
                        rgba: self.background_color
                    Rectangle:
                        size: self.size
                        pos: self.pos

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, you can avoid writting the same code for every Label. As all your labels have the same style, you may create a custom Label class in your .py file:

from kivy.uix.label import Label # Don't forget to import Label class

class CustomLabel(Label):
    pass

Then, in your .kv file customize that class:

<CustomLabel>:
    background_color:(150/255, 150/255, 150/255, 1)
    text:"table"
    canvas.before:
        Color:
            rgba: self.background_color
        Rectangle:
            size: self.size
            pos: self.pos

Now, you’re able to call CustomLabel in your .kv file, instead of the whole code for every label. The example below produces the same result you already have.

<Win>
    box1:box1
    BoxLayout:
        size: root.size
        orientation: "vertical"
        BoxLayout:
            size_hint: 1, 5
            ScrollView:
                GridLayout:
                    id:box1 
                    orientation: 'tb-lr'
                    height: self.minimum_height
                    size_hint_y: None
                    row_default_height:180
                    spacing: 2
                    cols:1
                    CustomLabel:
                    CustomLabel:
                    CustomLabel:
                    CustomLabel:

As you can see you only have to call CustomLabel:.

However, if you pretend to add a lot of Labels, the best way is to use a for loop within your python file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading