Search code examples
pythonkivytextinput

Restrict kivy's TextInput to only ascii characters


Please is there a way i can make a TextInput to disallow non ascii characters. So that when text is entered into the TextInput if a non-ascii character is typed it will not be added to TextInput. Just like the way one uses int filter, thus only whole numbers will be allowed in the TextInput
Please an example code would very be helpful. Thanks in advance


Solution

  • one potential solution is to use .decode() with the errors='ignore' flag on a string (eg of textinput). for example:

    "food ресторан".decode("ascii", errors='ignore')
    

    will replace all the chars it can to ascii silently

    EDIT** updated example with przyczajony's suggestion to use filters:

    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)