Tkinter text entry validation in python Tkinter text entry validation in python tkinter tkinter

Tkinter text entry validation in python


def calculate():    name = (textboxName.get())    age = (textboxAge.get())    try:        newAge = int(age)+5    except ValueError:        tkinter.messagebox.showinfo("Error", "The Intended Reading Age is invalid")        textboxAge.delete("0","end")        return    print("Hello",name)    print("In 5 years time you will be ",newAge)    # ...

If an error occurs somewhere in the try-section, python will not crash, but rather jump to the except part. The critical step is converting age into integer. This throws a ValueError if it is a string. In this case, the message box is shown and the text in your textbox is deleted. return then will stop the function, so the rest of it won't be processed. If nothing happens in the try-section, then except will be skipped.