1

I have created a simple program which asks for a user's name and age. The program will then take the details from a textbox and work out how old they will be in 5 years time.

The interface is fine. It's the validation that I am having difficulty with. When a user enters a letter instead of a number the program shows an error message, but continues to run regardless. I have tried using a while True: loop but this seems to just crash the program.

Here's what I have written already:

def calculate():
    name = (textboxName.get())
    age = (textboxAge.get())

    if age.isalpha():
        tkinter.messagebox.showinfo("Error", "The Age is invalid")
        textboxAge.delete("0","end")

    newAge = int(age)+5
    print("Hello",name)
    print("In 5 years time you will be",newAge)

I have looked at a few other tutorials but they are a little confusing. I am going to extend this by adding another elif in and the following code

 elif age >= 100:
        tkinter.messagebox.showinfo("Error", "You have entered a number greater than 100")
        textboxAge.delete("0","end")

but this doesn't like the fact it is a string not an integer.

  • What would be the best way to check to see if a number has been entered into a textbox?
Neos Nokia
  • 125
  • 1
  • 2
  • 11
  • 1
    The pythonic way would be to `try` and `except`. Try to add 5 to the integer and if this throws a `ValueError`, pop up a messagebox and `return` the function with None. – offeltoffel Nov 03 '17 at 16:47
  • Thanks for this. I have just tried this and whilst it works when you enter 5, if you enter a string the shell returns this: File "/Volumes/KINGSTON/gui validaiton.py", line 24, in calculate newAge = int(age)+5 ValueError: invalid literal for int() with base 10: 'd' – Neos Nokia Nov 03 '17 at 16:51
  • I added an answer to your question. Please try again with this code and report to us if this solves your problem. – offeltoffel Nov 03 '17 at 16:55
  • Hi, thank you for this, yes it does, however, if I wanted to check to see if the person's name was entered could this be added in too? for example, if i was going to check the length of someone's name as well as if it was letters or numbers added in – Neos Nokia Nov 03 '17 at 20:43
  • Sorry, I don't get that. Could you add an example to your question, please? – offeltoffel Nov 03 '17 at 22:32
  • I'd check Based on [this](https://stackoverflow.com/a/46260513/7032856) for filtering a string and [that](https://stackoverflow.com/a/4140988/7032856) for entry widget validation. – Nae Nov 04 '17 at 00:20

1 Answers1

1
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.

offeltoffel
  • 2,691
  • 2
  • 21
  • 35
  • 1
    This answer would be better if you explained what you did differently. Otherwise the reader has to compare your code to the original, line by line and character by character. – Bryan Oakley Nov 03 '17 at 17:09
  • Sorry, I did that in my comment, before adding some code to my explanation. – offeltoffel Nov 03 '17 at 22:28