Skip to content
Advertisement

How to stack multi-line labels vertically in BoxLayout

I am trying to create a stack of multiline Labels on Kivy inside a BoxLayout. How do I get the BoxLayout to expand according to its contents? Now the BoxLayout is squeezing the Labels instead.

Desired vs Actual Effect

I do not wish to hardcode the multiline Label size as I want each to be flexible to accommodate varying lines of text.

My demo code is as follows:

JavaScript

Advertisement

Answer

If the Labels you added to MyWidget have fixed amount of text you can just set a specific value for MyWidget‘s height as self.height = "150dp" (Adjust to your need).

Otherwise if you want the MyWidget‘s height enough to contain its children (here, Labels) then set it to its minimum_height. Also you have to set each Label‘s height to its texture height.

Thus your modified MyWidget class should now look like,

JavaScript

You can also utilize padding and spacing as,

JavaScript

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement