【问题标题】:Restrict kivy's TextInput to only ascii characters将 kivy 的 TextInput 限制为仅 ascii 字符
【发布时间】:2016-05-25 04:41:31
【问题描述】:

请问有什么方法可以让 TextInput 禁止非 ascii 字符。因此,当文本输入到 TextInput 时,如果键入了非 ascii 字符,则不会将其添加到 TextInput。就像使用 int 过滤器的方式一样,因此 TextInput
中只允许整数 请提供示例代码非常有帮助。提前致谢

【问题讨论】:

    标签: python kivy textinput


    【解决方案1】:

    TextInput 过滤在文档中进行了描述,即使有示例:Filter

    使用正则表达式检查输入的字符串是否包含所需字符 ([A-Za-z0-9 ])。如果通过,则返回字符串。

    【讨论】:

      【解决方案2】:

      一种可能的解决方案是使用带有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)
      

      【讨论】:

      • 感谢 4 d 回复。但是我仍然不清楚如何将它添加到 d TextInput 小部件,我还是 kivy 和 python 的新手。你能告诉我怎么做吗?再次感谢...
      • 大家好,我真的非常感谢你们的帮助。特别是@brmbigbang。感谢您提供的示例,它们绝对完美。我的应用程序现在几乎完成了,我需要它......而且我必须说堆栈溢出真的很摇滚。再次感谢您
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多