Kivy button text alignment issue

You need to set text_size property, something like:

btn.text_size = (290, 40)

If you want to avoid numbers in text.size, then try this:

text_size: self.size


The documentation of Button starts with "A Button is a Label". Even for Widgets that don't mention their lineage explicitly, you should take a note of the second line in the API doc of the Widget in question. In this case "Bases: kivy.uix.label.Label".

This establishes that the button inherits from a Label. (I am explicitly mentioning this because this part of looking at the base Class's inherited properties sometimes is not intuitive for everyone).

If you look at the Docs for label, specifically the halign property, it asks you to utilize text_size to achieve proper text alignment. What this means is the text is aligned inside a bounding box that is set by the text_size property. This property can be set to be:

a) The size of the Widget. text_size: self.size

b) Less than the size of the size of the widget (what you are looking for) text_size: self.width - dp(10), self.height - dp(10)

c) Unconstrained on one of the sides text_size: self.width, None

d) or both text_size: None, None

e) or constrained to a different Widget text_size: other_button.size

The reason for using text_size is to give more control to the user. You should also look at the textalign example

Tags:

Python

Kivy