3

I have a TextInput in Kivy with long content. I want to know the width of the TextInput in 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

Assem
  • 11,574
  • 5
  • 59
  • 97
  • How about using `cursor_col` & `cursor_row`: http://kivy.org/docs/api-kivy.uix.textinput.html#kivy.uix.textinput.TextInput.cursor_col ? – Iron Fist Jul 06 '15 at 02:21
  • @KhalilAmmour-خليلعمور I think cursor_col gives you the position of cursor not the width of the line. – Assem Jul 06 '15 at 09:13
  • yes that's my point, do a combination check between `cursor_col` & `cursor_row` to store value of `cursor_col` and or next new line when `cursor_row` = +1 then that should be the length of the line, did you get my point here? – Iron Fist Jul 06 '15 at 12:32
  • I got your point, this is a hack. but not sure if it works well. – Assem Jul 06 '15 at 13:40
  • Well then, may be you can implement it as function and post it for kivy module... :) – Iron Fist Jul 06 '15 at 13:42

2 Answers2

1

You can check lines of TextInput using its _lines property. To get their lengths 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()
Nykakin
  • 8,657
  • 2
  • 29
  • 42
  • Thank you . That gives the number of characters in that line not the total width. – Assem Jul 06 '15 at 20:30
  • 1
    You can't get total value since characters doesn't have fixed widths, for example . character has smaller width than X. Use that code to compare width of lines that consist only of . and X characters. – Nykakin Jul 06 '15 at 20:38
0

Just to improve this discussion: In TextInput._lines_labels is field size (tuple) [0] with width of line. Each line. Just to iterate and look for the biggest and 'voilà'. This works with any font (prop or not).

Radek

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50