【发布时间】:2016-05-25 04:41:31
【问题描述】:
请问有什么方法可以让 TextInput 禁止非 ascii 字符。因此,当文本输入到 TextInput 时,如果键入了非 ascii 字符,则不会将其添加到 TextInput。就像使用 int 过滤器的方式一样,因此 TextInput
中只允许整数
请提供示例代码非常有帮助。提前致谢
【问题讨论】:
请问有什么方法可以让 TextInput 禁止非 ascii 字符。因此,当文本输入到 TextInput 时,如果键入了非 ascii 字符,则不会将其添加到 TextInput。就像使用 int 过滤器的方式一样,因此 TextInput
中只允许整数
请提供示例代码非常有帮助。提前致谢
【问题讨论】:
TextInput 过滤在文档中进行了描述,即使有示例:Filter
使用正则表达式检查输入的字符串是否包含所需字符 ([A-Za-z0-9 ])。如果通过,则返回字符串。
【讨论】:
一种可能的解决方案是使用带有errors='ignore' 标志的.decode() 字符串(例如textinput)。例如:
"food ресторан".decode("ascii", errors='ignore')
将所有字符替换为 ascii 静默
EDIT** 更新了 przyczajony 建议使用过滤器的示例:
class AsciiInput(TextInput):
def insert_text(self, string, from_undo=False):
string = string.decode("ascii", errors='ignore')
return super(AsciiInput, self).insert_text(string, from_undo=from_undo)
【讨论】: