Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to limit the character in Entry

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading