''' 20210923 R. Dawes New features added in this version: In the previous version we used "float()" to convert the input from string to float. But if the input string is something like "12a?78..5" then trying to convert it to a float will cause an execution error. We can catch the error and recover from it using the "try" and "except" controls. In use, they look like this: try: except : In our situation, we are possibly going to be applying "float()" to a string that cannot be translated to a number. This causes an error (technically called an exception) called ValueError, so this is what we look for. See the use of this in the "button_1_clicked" function below ''' import tkinter as tk import sys window = tk.Tk() window.title("Compare two numbers") window.geometry("1200x800") label_1 = tk.Label(window,text="Enter x in this box") #add an entry box to accept input entry_1 = tk.Entry(window, width = 10) label_2 = tk.Label(window,text="Enter y in this box") #add an entry box to accept input entry_2 = tk.Entry(window, width = 10) #add a text box to show output text_1 = tk.Text(window,height = 10, width = 30) #add a button - a clickable object #add a function to react to button_1 def button_1_clicked(): try: x = float(entry_1.get()) except ValueError: x = None # if the input cannot be interpreted as a number # we assign x the value of None. Later we will check # x to see if it equals None. If so, we know the input # was invalid. try: y = float(entry_2.get()) except ValueError: y = None text_1.delete(1.0, tk.END) if (x == None) or (y == None): # here we check to see if the input was # invalid. If it was invalid, we put a # message in the Text box text_1.insert(tk.END, "Invalid Input") else: # now we know the input was successfully converted to floating point numbers if (x < y): text_1.insert(tk.END, str(x) + " < " + str(y)) elif (x > y): text_1.insert(tk.END, str(x) + " > " + str(y)) else: text_1.insert(tk.END, str(x) + " = " + str(y)) button_1 = tk.Button(window, text = "Compare Them", command = button_1_clicked) #add a button to end execution button_2 = tk.Button(window,text = "Exit", command = sys.exit) label_1.grid(row = 0, column = 0) entry_1.grid(row = 0, column = 1) label_2.grid(row = 1, column = 0) entry_2.grid(row = 1, column = 1) button_1.grid(row = 2, column = 0) text_1.grid(row = 2, column = 2) button_2.grid(row = 75, column = 869) # getting a bit carried away with the row and column window.mainloop()