设置textinput的width属性 [英] Set the width property for textinput

查看:96
本文介绍了设置textinput的width属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以与展示应用程序相同的方式创建了一个小部件.但是,我在api 文档中看不到任何东西来限制宽度输入字段.如何设置宽度?

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

推荐答案

通过width我假设您的意思是文本长度,因为设置小部件的width与设置height一样容易

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)


如果您恰巧正在运行Kivy的开发版本(1.8.1-dev,来自git),则这甚至更加容易,并且可以通过kv完成.您可以使用可调用的

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

这篇关于设置textinput的width属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆