How to limit the character in Entry

Question:

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.

Answers:

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
Answered By: Tls Chris
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.