fire_water
fire_water

Reputation: 1449

Kivy Textinput Wrap

How do I configure a Kivy text input box such that it wraps the text I paste into it? For example, let's say I have a string that is 1,000 letters long and I paste it into a text input box which has multiline enabled. Instead of wrapping to the next line, the pasted text is displayed as one line which runs beyond the width of the window. Very unexpected behavior.

A string pasted into TextInput with id seq_input_box

UUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUU

.kv file:

MinRoot:

<MinRoot>:
    MyForm

<MyForm>:
    orientation: "vertical"
    seq_input: seq_input_box
    BoxLayout:
        height: "40dp"
        size_hint_y: None
        Button:
            text: "Go!"
            on_press: root.calc_seq()
    TextInput:
        id: seq_input_box
        focus: True

Result:

enter image description here

Thanks

Upvotes: 3

Views: 1629

Answers (1)

fire_water
fire_water

Reputation: 1449

@zeeMonkeez, thank you for looking into the root cause of this problem. Here is the solution I ended up using:

.py file:

class MyTextBox(TextInput):

    def insert_text(self, substring, from_undo=False):

        line_length = 50

        # Remove all whitespace in string.
        seq = ''.join(substring.split())

        # For every line_length characters, insert a newline character.
        if len(seq) > line_length:

            # Splits seq by every Nth character and creates a list.
            # Example: [abc,def,ghi]
            # Then join the list items together using a newline character
            # as the separator.
            seq = '\n'.join([seq[i:i+line_length] for i in range(0, len(seq), line_length)])

        return super(MyTextBox, self).insert_text(seq, from_undo=from_undo)

More info about insert_text is available here.

Upvotes: 1

Related Questions