Kivy Textinput Wrap

时间:2016-02-24 20:02:36

标签: python kivy

如何配置Kivy文本输入框,使其包装我粘贴到其中的文本?例如,假设我有一个长度为1,000个字母的字符串,并将其粘贴到启用了multiline的文本输入框中。粘贴的文本显示为一行超出窗口宽度,而不是换行到下一行。非常意外的行为。

粘贴到ID为seq_input_box

的TextInput的字符串
UUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUUUUCUUAUUU

.kv文件:

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

结果:

enter image description here

由于

1 个答案:

答案 0 :(得分:1)

@zeeMonkeez,感谢您查看此问题的根本原因。这是我最终使用的解决方案:

.py文件:

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)

有关insert_text的更多信息,请here