0

I am building a GUI BMI Calculator app using Python and tkinter and I need some help.

Since there must be no special characters in the user given string, I want an efficient way to detect special characters in a string.

I used regex compile method as I saw online. But it did not detect all the special characters. As when I gave 165,56 instead for 165.56, it returned and error in console. Other characters like #$@!$ etc, all worked perfectly showing tkMessageBox as I programmed.

I've tried each character in string iterating through and finding if special characters are present and I even used regex compile as mentioned above but none satisfies my queries.

    regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]')

#assume have already got the value for h from user as 165,65
    h = Entry.get(heightEntry)
    if not h:
        h = "0"
        height = 0
    elif h.isdigit() == True:
        height = float(h)
    elif h.isalnum() == True:
        height = "None"
    elif regex.search(h) == None:
        for x in h:
            if "." in x:
                y += 1
        if y >=2:
            height = "None"
        else:
            height = float(h)
    else:
        height = "None"

#Check height lies between 0.5m and 3.0m or 50cm to 300cm
    if not height:
        print("There is no value for height to calculate")
        tkMessageBox.showerror("Alert","No height value")
    else:
        if height == "None":
            print("Invalid height value")
            tkMessageBox.showerror("Alert","Invalid height value")
            height = 0
        elif not ((50.0<=height<=300.0) or (0.5<=height<=3.0)):
            print("Invalid height value",height)
            tkMessageBox.showerror("Alert","Invalid height value")
        else:
            if 50.0<=height<=300.0:
                print("Height:",height,"cm")
            else:
                print("Height:",height,"m")

I expected the outcome to show the message box below height == "None"

But it showing traceback most recent call error.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • I think you're thinking way too complicated. You want a user to be able to specify a number either with `.` or `,` as a decimal seperator? Then rather than _looking_ for "special" characters, just try to parse the input as a number, having replaced the `,` with a `.`: `try: height = float(h.replace(",", ".")); except ValueError: height = None` – L3viathan Sep 29 '19 at 10:19
  • No I wanted to ask the user to enter details as either whole numbers (e.g., 165, 256, etc.) or as floating points(e.g.,165.65, 65.5) and if user gives wrong inputs, like going crazy( 56$#$#$ or 165,65) the program should show tkmessagebox as invalid input – Techno-Sachin Sep 29 '19 at 12:06

1 Answers1

1

You can use validatecommand to check whether the input string is a valid float. You can read this post for details.

import tkinter as tk

root = tk.Tk()

def onValidate(P):
    try:
        float(P)
    except:
        return False
    return True

vcmd = (root.register(onValidate), '%P')

entry = tk.Entry(root, validate="key", validatecommand=vcmd)
entry.pack()

root.mainloop()
Henry Yik
  • 22,275
  • 4
  • 18
  • 40