from tkinter import * # Trial - Entry Box # Purpose: Learn how the Entry widget works - creating it, # styling it for errors, and clearing it. # This is the same pattern used in the Distance Converter. class EntryTrial: def __init__(self): self.frame = Frame(padx=10, pady=10) self.frame.grid() self.heading = Label(self.frame, text="Entry Box Trial", font=("Arial", "16", "bold")) self.heading.grid(row=0) instructions = ("Press Simulate Error to see the entry turn pink " "and clear. Press Reset to restore it.") Label(self.frame, text=instructions, wraplength=300, font=("Arial", "11"), fg="#666666").grid(row=1, pady=5) # --- The entry box --- # font sets the text size inside the box # width sets how many characters wide it appears self.dist_entry = Entry(self.frame, font=("Arial", "14"), width=20) self.dist_entry.grid(row=2, padx=10, pady=10) # --- Answer / error label --- # Starts red (error state) - same as the Distance Converter self.answer_error = Label(self.frame, text="Please enter a number", font=("Arial", "13", "bold"), fg="#9C0000") self.answer_error.grid(row=3, pady=5) # --- Buttons --- btn_frame = Frame(self.frame) btn_frame.grid(row=4, pady=10) # Read button - shows what was typed in the entry Button(btn_frame, text="Read Entry", font=("Arial", "11", "bold"), bg="#004C99", fg="#FFFFFF", width=18, command=self.read_entry).grid(row=0, column=0, padx=5, pady=4) # Simulate error - turns entry pink and clears it Button(btn_frame, text="Simulate Error", font=("Arial", "11", "bold"), bg="#9C0000", fg="#FFFFFF", width=18, command=self.simulate_error).grid(row=1, column=0, padx=5, pady=4) # Reset - puts entry and label back to normal Button(btn_frame, text="Reset / Clear", font=("Arial", "11", "bold"), bg="#666666", fg="#FFFFFF", width=18, command=self.reset_entry).grid(row=2, column=0, padx=5, pady=4) def read_entry(self): """ Reads whatever is currently typed in the entry box. Only accepts positive whole numbers and decimals. - Non-numeric input is caught by the ValueError exception - Negative numbers are caught by the value < 0 check - Zero and positive numbers (including decimals) are accepted """ raw = self.dist_entry.get() # Reset entry box colour first self.dist_entry.config(bg="#FFFFFF") try: value = float(raw) if value < 0: # Number was entered but it is negative self.answer_error.config( text="Please enter a positive number", fg="#9C0000" ) self.dist_entry.config(bg="#F4CCCC") self.dist_entry.delete(0, END) else: # Valid positive number or zero self.answer_error.config( text=f"You entered: {value}", fg="#004C99" ) except ValueError: # Input could not be converted to a number at all self.answer_error.config( text="Please enter a number, not text", fg="#9C0000" ) self.dist_entry.config(bg="#F4CCCC") self.dist_entry.delete(0, END) def simulate_error(self): """ When validation fails in check_distance(), two things happen: 1. Entry background turns pink (#F4CCCC) 2. Entry is cleared using .delete(0, END) - 0 means start from the first character - END is a tkinter constant meaning 'the last character' """ self.answer_error.config( text="Error! Entry cleared and turned pink.", fg="#9C0000" ) self.dist_entry.config(bg="#F4CCCC") self.dist_entry.delete(0, END) def reset_entry(self): """ The clear_entry() method in the Distance Converter does this - resets the background to white and clears any text. Also resets the label back to the default error state. """ self.dist_entry.delete(0, END) self.dist_entry.config(bg="#FFFFFF") self.answer_error.config( text="Please enter a number", fg="#9C0000", font=("Arial", "13", "bold") ) # main routine if __name__ == "__main__": root = Tk() root.title("Entry Box Trial") EntryTrial() root.mainloop()