How to get width in Kivy TextInput or Label characters

I have TextInputin Kivywith long content. I want to know the width TextInputin characters. In other words, the length of the lines?

textinput = TextInput(text="Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps")

enter image description here

+4
source share
1 answer

You can check strings TextInputwith a property _lines. To use their length, use len()builtin:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

from kivy.lang import Builder

Builder.load_string("""

<MyWidget>:
    Button:
        text: "Print width"
        on_press: print([len(line) for line in ti._lines])
    TextInput
        text: "Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps"
        id: ti
""")

class MyWidget(BoxLayout):
    pass

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()
+1
source

All Articles