Set the width property for textinput

99封情书 提交于 2019-12-02 08:56:38

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/25296508/set-the-width-property-for-textinput

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!