Home > front end >  Why does tkinter think that integer is string
Why does tkinter think that integer is string

Time:04-12

Continuing from yesterday, i was implementing a basic exception handler, to deal with every type of error from the user, using conditional if and elif statements. It was fine until my program wrongly thinks that integer inputs are non-integers (for the age field), and this stops me from continuing my program, as other functions are dependant on this. Here is the code snippet:

def submit():
        username = UserName.get()
        firstname = User_FirstName.get()
        surname = User_Surname.get()
        age = User_Age.get()
        height = User_Height.get()
        weight = User_Weight.get()
        data = [username, firstname, surname, age, height, weight]
flag = False
        while flag == False:
            if len(username) == 0:
                messagebox.showerror('Project Pulse', 'Please ensure the "Username" field is not left blank')
                break
            elif type(firstname) != str:
                messagebox.showerror('Project Pulse', 'Please ensure there are no numbers in the "First Name"')
                break
            elif len(firstname) == 0:
                messagebox.showerror('Project Pulse', 'Please ensure that the "First Name" field is not left blank')
                break
            elif len(surname) == 0:
                messagebox.showerror('Project Pulse', 'Please ensure that the "Last Name" field is not left blank')
                break
            elif type(surname) != str:
                messagebox.showerror('Project Pulse', 'Please ensure there are no numbers in the "Surname"')
                break
            elif len(age) == 0:
                messagebox.showerror('Project Pulse', 'Please ensure the "age" field is not left blank')
                break
            elif type(age) != int:
                messagebox.showerror('Project Pulse', 'Please ensure only integers are input in "Age"')
                break
...
 else:
                flag = True

Here, submit is a button. The elif statements continue for a few more lines, but the point is, the program does not run past the 'age' line, i.e. the error message box : 'Please ensure only integers are input in "Age" displays, no matter what. I tried printing the actual age variable, and i got an integer, so I can't seem to find the issue!

CodePudding user response:

I think your issue might be that get() treats every user input as a string.

In other words, even though the user might want to say 22, the computer would read it as "22".

To safeguard against this, you can try putting int() around User_Age.get():

int(User_Age.get())

Hope this helps! -VDizz

CodePudding user response:

Like @VDizz, I am assuming your values are coming from tk.Entry widgets. Therefore all values will be strings. So the casting will work but this will also generate an error in your elif block. To avoid the error causing a new issue you might try writing a small function to check the type. As per this stack overflow response https://stackoverflow.com/a/9591248/6893713.

Hope this helps too.

In response to @Guilherme Iazzete's comment (slightly modified from the link):

    def intTryParse(value):
        try:
            int(value)
            return True
        except ValueError:
            return False

    username = 'HarvB'
    age = '4'
    flag = False
    while flag == False:
        if len(username) == 0:
            messagebox.showerror('Project Pulse', '...')
            break
            ...
        elif intTryParse(age) is False:
            messagebox.showerror('Project Pulse', '...')
            break
            ...
        else:
            flag = True
  • Related