3

I am new in Kivy programming. I want to enable only 4 lines in text Input in Kivy. I can use either only one line either multi lines, but I want a text field which has enabled only 4 lines. In short I want a text field where i can enter anything till to four rows

Akash D G
  • 177
  • 1
  • 6
  • 18

3 Answers3

4

As far as I know, there is no clean way to do that in kivy. You can try using 4 seperate TextInputs and just switching focus when user hits Enter. Here's an example:

from kivy.base import runTouchApp
from kivy.lang import Builder

runTouchApp(Builder.load_string("""
BoxLayout:
    orientation: "vertical"
    TextInput:
        multiline: False # one line only
        on_text_validate: t1.focus = True # when Enter is pressed, switch focus
    TextInput:
        id: t1
        multiline: False
        on_text_validate: t2.focus = True
    TextInput:
        id: t2
        multiline: False
        on_text_validate: t3.focus = True
    TextInput:
        id: t3
        multiline: False
            """))
Edvardas Dlugauskas
  • 1,469
  • 1
  • 12
  • 14
2

You could set a counter for the line breaks and/or characters and block any new line break/character.

Kivy TextInput documentation

You can use the row value. This could be managed by the filtering function of TextInput.

Edit:
A little example class:

class TInput(TextInput):

    def __init__(self,**kwargs):
        super(TInput,self).__init__(**kwargs)
        self.__lineBreak__=0

    def insert_text(self, substring, from_undo=False):
        if "\n" in substring and self.__lineBreak__ <= 4:
            self.__lineBreak__ += 1
            self.__s__ = substring
        elif self.__lineBreak__ > 4 and "\n" in substring:
            self.__s__ = ""
        return super(TInput, self).insert_text(s, from_undo=from_undo)

I think that should do the job

user8128255
  • 133
  • 3
  • 10
0

In 1.11.0 Kivy version, you can simply add multiline=False

  TextInput(font_size=20,
            multiline=False,
            height=40)
Ivan Talalaev
  • 6,014
  • 9
  • 40
  • 49