I need help limiting the amount of characters in python kivy

Asked

Viewed 1,333 times

0

I’m with a small amount, I’m trying to limit the amount of characters that can be typed without text, for example, a DDD code, where it only takes 2 or 3 digits, limit to only a maximum of 3 numbers, but I’m not getting, I tried to accomplish from the second, but it didn’t work either, the idea was now to limit by the kivy script itself, thanks 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'

Code python dd = self.root.ids.dd.text

dd = maxlength (2)

1 answer

1


The guy string is iterable 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 from the beginning of the text:

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

Already, if the value reported after the two points exceeds the text size, it will be returned only until the end of the same:

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

Thus, to limit a text to a number N character, just do texto[:N].

Additional reading:

OBS:

In your case I think the code you want would be +- like this :

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

-Input: 0011111111

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

-Output: DDD: 00

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

-Exit: number: 11111111

recommend making a regex or a mask in your input so there is no risk of the user typing wrong

  • If the answer is the same, you just need to signal the question as duplicate. There is no reason to have multiple responses with the same content spread around the site. Just below the question there is the "flag" button, just ask that the question should be closed because it is duplicated and indicate the link of the other question.

  • Thanks I’m a new user, I still don’t know how Stack works

Browser other questions tagged

You are not signed in. Login or sign up in order to post.