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 - v1", 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. Returns an empty string if nothing is typed. """ value = self.dist_entry.get() if value == "": self.answer_error.config( text="Entry is empty - nothing typed yet", fg="#9C0000" ) else: self.answer_error.config( text=f"You entered: '{value}'", fg="#004C99" ) 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()