I wrote this Tkinter mock up Tkinter Application, that checks if the "Price" is a number (int, float) or not, and displays a message accordingly, I have commented at some of the essential sections and variables, hope this helps.
from tkinter import *
global root, canvas, ef, is_int, is_float, error, box, price_type_is_number
# Setting Our variables that we will use later to None for the time being
is_int = None
is_float = None
error = None
box = None
price_type_is_number = None # This is the final result, if it's a number, Float, int, or not.
# Setting up the Tk Window and Canvas
root = Tk()
canvas = Canvas(width=500, height=500)
canvas.pack()
# Functions
def submit():
global root, canvas, ef, is_int, is_float, error, box, price_type_is_number
price_type_is_number = None
if not is_int and not is_float: # If it's not a float and not an int it displays an error
try: # Just Tries to remove any previous instances of the box element, or error element to avoid lag and overlapping
box.destroy()
error.destroy()
except:
pass
box = Label(bg="gray75", width=20, height=5)
price_type_is_number = False # You can use this value to get the definite status if the price is a number, float, int. But here it sets it to False
error = Label(text="Price must be a number", fg="red", bg="gray75")
canvas.create_window(165, 200, anchor="nw", window=box)
canvas.create_window(165, 210, anchor="nw", window=error)
elif is_int or is_float: # If it's a float, or int it displays the correct message
try: # Just Tries to remove any previous instances of the box element, or error element to avoid lag and overlapping
box.destroy()
error.destroy()
except:
pass
price_type_is_number = False # You can use this value to get the definite status if the price is a number, float, int. But here it sets it to True
box = Label(bg="gray75", width=20, height=5)
error = Label(text="Price is a number", fg="green", bg="gray75")
canvas.create_window(165, 200, anchor="nw", window=box)
canvas.create_window(165, 210, anchor="nw", window=error)
def process():
global root, canvas, ef, is_int, is_float
try: # checks to see if it can convert the str into a float
f_test = float(ef.get())
is_float = True # If it can it sets it's float status to true
except ValueError: # If it can't sets it's float status to false
is_float = False
try: # Checks to see if it can convert the str into an int
int_test = int(ef.get())
is_int = True # If it can sets the int status to true
except ValueError: # if it can't sets it to false
is_int = False
submit() # Runs the submit command
# Widgets
efl = Label(text="Price") # Label For Entry Field
ef = Entry() # Entry Field
efb = Button(text="Enter", command=process)# Button to submit
# Adding Widgets to Canvas
canvas.create_window(165, 150, anchor="nw", window=ef)
canvas.create_window(165, 110, anchor="nw", window=efl)
canvas.create_window(315, 145, anchor="nw", window=efb)
mainloop()