Search code examples
pythonkivykivy-language

Kivy python have label text fit screen width even after changing text


In my code, I used texture_size:self.size to have the text in my label fit the width of the screen fully. It worked, but when I updated the text of the label it stopped getting displayed properly. I even tried using texture_update, but it didn't work. I'm not very experienced with kivy, so perhaps I'm missing a solution here. I did find some Scale label thing online, but it seemed way too complicated, and so I'm wondering if there is a simple way to achieve what I want. Thanks for your time. Edit: I was able to achieve my goal. Basically my app uses multiple screens, so before I created this screen when the user clicks the button to come here, and remove it when they go back. I'm leaving this up just in case someone else has a similar issue.


Solution

  • I believe you want text_size: self.size texture_size in read only. Make sure sure size_hint is also set to an appropriate value, a commonly useful combination of settings is

    Label:
        size_hint_y: None
        text_size: self.width, None
        height: self.texture_size[1]
    

    which means:

    • the widget sets its own height
    • the texture should be as large as the available horizontal space
    • the height should be the height of the resulting texture

    which could be good default, as its often what people need, but making them default would constrain people who want something else.