KDEx
KDEx

Reputation: 3657

Set the width property for textinput

I have created a widget in the same fashion as the showcase application. However I do not see anything in the api documentation to limit the width of the input field. How can I set the width?

CTextInput:
    size_hint_y: None
    height: '32dp'
    multiline: False
    hint_text: 'SNMP Community String(s)'

Upvotes: 1

Views: 3692

Answers (1)

kitti
kitti

Reputation: 14794

By width I'm assuming you mean text length, as setting the width of a widget is just as easy as setting the height is.

There is an example of text filtering in the documentation you linked which could easily be modified to limit the length:

class MyTextInput(TextInput):
    def insert_text(self, substring, from_undo=False):
        # limit to 5 chars
        substring = substring[:5 - len(self.text)]
        return super(MyTextInput, self).insert_text(substring, from_undo=from_undo)


If you happen to be running the development version of Kivy (1.8.1-dev, from git), this is even easier and can be done from kv. You can limit the length of the text with a callable input_filter. Here's a quick example:

TextInput:
    # limit to 5 chars
    input_filter: lambda text, from_undo: text[:5 - len(self.text)]

Upvotes: 8

Related Questions