''' 20210923 R. Dawes ''' import tkinter as tk 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(): x = entry_1.get() # this is how we get input the user entered in the y = entry_2.get() # Entry boxes on the screen text_1.delete(1.0, tk.END) # tk.END is a special object that always reaches the # last occupied location in a Text widget # This instruction clears the Text box # In class I mentioned that I did not know why the first occupied location in a Text # widget is referenced by "1.0" .... but now I do know! In a Text widget, the lines are # numbered starting at 1, and the columns are numbered starting at 0. So the "1.0" is # not a floating point number. The number before the decimal point indicates the line # number, and the number after the decimal point indicates the column number. So # "1.0" means "row 1, column 0", which in this case - unlike almost EVERY other # instance of indexing in Python, including the tkinter layout grid - means the # first position in the first row. # This is the strangest thing I have seen in a long time. First, why would you start # line numbers at 1 and column numbers at 0? Second, why would you use a decimal point # instead of a comma to separate the row and column values? if (x < y): text_1.insert(tk.END, x + " < " + y) elif (x > y): text_1.insert(tk.END, x + " > " + y) else: text_1.insert(tk.END, x + " = " + y) # This function gives the expected output when the input is 8 and 9, but # if the input is 8 and 10, it seems to get the comparison wrong. Why? See the # next example to find out. button_1 = tk.Button(window, text = "Compare Them", command = button_1_clicked) 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) window.mainloop()