PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 19, 2022

[FIXED] Why does tkinter think that integer is string

 July 19, 2022     error-handling, integer, python, tkinter     No comments   

Issue

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!


Solution

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


Answered By - HarvB
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing