0

I need to limit the entry to a maximum of 5 digits.

def validate(S):
    try:
        float(S)
        return True
    except ValueError:
        messagebox.showerror(message="Datos erroneos, únicamente números.", title="ERROR")
        return False

e1 = tk.Entry(master, validate="key", validatecommand=(master.register(validate), '%S'))

I have this method to receive and validate that it's just numbers at the entry, but i would like to know how can I put a max limit of 5 digits on the entry.

I tried with the methods used in this question (Tkinter entry character limit) but it is not working with the validate method.

1 Answers1

3

Return False from validate if the length of the string is more than 5.

def validate(S):
    try:
        float(S)
        return len(S) <= 5
    except ValueError:
        messagebox.showerror(message="Datos erroneos, únicamente números.", title="ERROR")
        return False
Tls Chris
  • 3,564
  • 1
  • 9
  • 24