Akash D G
Akash D G

Reputation: 188

Enable only four line in text Input in Kivy

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

Upvotes: 3

Views: 1844

Answers (3)

Ivan Talalaev
Ivan Talalaev

Reputation: 6494

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

  TextInput(font_size=20,
            multiline=False,
            height=40)

Upvotes: 0

user8128255
user8128255

Reputation: 132

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

Upvotes: 2

Edvardas Dlugauskas
Edvardas Dlugauskas

Reputation: 1489

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
            """))

Upvotes: 4

Related Questions