设置文本输入的宽度属性 [英] Set the width property for textinput

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

问题描述

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

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

推荐答案

By 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 完成.您可以使用可调用的 input_filter.这是一个简单的例子:

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

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

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