I need help limiting the amount of characters in kivy python

0

I'm having a small amount, I'm trying to limit the amount of characters that can be typed without text, for example a DDD code, where only 2 or 3 digits, limit to only 3 numbers, but I'm not I tried to run the second, but it also did not work, the idea was now to limit by the kivy script itself, thank you even more personal: D

Kivy Code TextInput:

    id: dd
    pos_hint:{"center_x":.35, "center_y":.2}
    size_hint:(.1,.05)
    multiline:False
    write_tab: False
    input_filter:'int'

Python code dd = self.root.ids.dd.text

dd = maxlength (2)

    
asked by anonymous 11.10.2018 / 21:46

1 answer

1

The string type is iterated in Python and allows you to access its content via slices . For example, texto[1:5] would return from the first to the fourth character of texto .

>>> print('anderson'[1:5])
nder

If you omit the first value, Python will understand that it is zero, starting at the beginning of the text:

>>> print('anderson'[:5])
ander

However, if the value entered after the colon exceeds the text size, it will only be returned to the end of the text:

>>> print('anderson'[1:30])
nderson

In this way, to limit a text to a number N of characters, just do texto[:N] .

Further Reading:

OBS:

In your case, I think the code you want would look like this:

n = input("Digite o numero com DDD: (sem caracteres especial)")

-Enter: 0011111111

print ('DDD:',n[:2])

-Saida: DDD: 00

print ('numero:',n[2:])

-Saida: number: 11111111

I recommend doing a regEx or a mask in your input so there is no risk of the user typing the wrong

    
12.10.2018 / 00:55