from tkinter import * import all_constants as c import conversion_rounding as cr class Converter: """ Distance conversion tool (km, m, cm, mm) """ def __init__(self): """ Distance converter GUI """ self.all_calculations_list = [] self.dist_frame = Frame(padx=10, pady=10) self.dist_frame.grid() self.dist_heading = Label(self.dist_frame, text="Distance Converter", font=("Arial", "16", "bold")) self.dist_heading.grid(row=0, columnspan=2) instructions = ("Please enter a distance below, choose the units " "to convert from and to, then press Convert.") self.dist_instructions = Label(self.dist_frame, text=instructions, wraplength=250, width=40, justify="left") self.dist_instructions.grid(row=1, columnspan=2) # Input row: entry + from-unit dropdown self.input_subframe = Frame(self.dist_frame) self.input_subframe.grid(row=2, pady=10) self.dist_entry = Entry(self.input_subframe, font=("Arial", "14"), width=14) self.dist_entry.grid(row=0, column=0, padx=(0, 10)) self.from_unit_var = StringVar(value=c.UNITS[0]) self.from_unit_menu = OptionMenu(self.input_subframe, self.from_unit_var, *c.UNITS) self.from_unit_menu.config(font=("Arial", "12"), width=5) self.from_unit_menu.grid(row=0, column=1) # "to" label + to-unit dropdown self.to_subframe = Frame(self.dist_frame) self.to_subframe.grid(row=3, pady=(0, 10)) to_label = Label(self.to_subframe, text="Convert to:", font=("Arial", "12")) to_label.grid(row=0, column=0, padx=(0, 10)) self.to_unit_var = StringVar(value=c.UNITS[1]) self.to_unit_menu = OptionMenu(self.to_subframe, self.to_unit_var, *c.UNITS) self.to_unit_menu.config(font=("Arial", "12"), width=5) self.to_unit_menu.grid(row=0, column=1) error = "Please enter a number" self.answer_error = Label(self.dist_frame, text=error, fg="#9C0000", font=("Arial", "14", "bold")) self.answer_error.grid(row=4) # Conversion, help and history / export buttons self.button_frame = Frame(self.dist_frame) self.button_frame.grid(row=5) # button list (button text | bg colour | command | row | column) button_details_list = [ ["Convert", "#009900", lambda: self.check_distance(), 0, 0], ["Clear", "#666666", self.clear_entry, 0, 1], ["Help / Info", "#CC6600", "", 1, 0], ["History / Export", "#004C99", "", 1, 1] ] # List to hold buttons once they have been made self.button_ref_list = [] for item in button_details_list: self.make_button = Button(self.button_frame, text=item[0], bg=item[1], fg="#FFFFFF", font=("Arial", "12", "bold"), width=12, command=item[2]) self.make_button.grid(row=item[3], column=item[4], padx=5, pady=5) self.button_ref_list.append(self.make_button) # retrieve 'history / export' button and disable it at the start self.to_history_button = self.button_ref_list[3] self.to_history_button.config(state=DISABLED) def check_distance(self): """ Checks distance is valid and either invokes calculation function or shows a custom error """ # Retrieve distance to be converted to_convert = self.dist_entry.get() # Reset label and entry box (if we have error) self.answer_error.config(fg="#004C99", font=("Arial", "13", "bold")) self.dist_entry.config(bg="#FFFFFF") error = "Please enter a valid positive number" has_errors = "no" try: to_convert = float(to_convert) if to_convert < 0: error = "Please enter a number greater than or equal to 0" has_errors = "yes" elif self.from_unit_var.get() == self.to_unit_var.get(): error = "Please choose two different units to convert between" has_errors = "yes" else: error = "" self.convert(to_convert) except ValueError: has_errors = "yes" # display error if necessary if has_errors == "yes": self.answer_error.config(text=error, fg="#9C0000") self.dist_entry.config(bg="#F4CCCC") self.dist_entry.delete(0, END) def convert(self, to_convert): """ Converts distances and updates answer label. Also stores calculations for Export / History feature """ from_unit = self.from_unit_var.get() to_unit = self.to_unit_var.get() answer = cr.convert_distance(to_convert, from_unit, to_unit) answer_statement = f"{to_convert} {from_unit} = {answer} {to_unit}" # enable history export button as soon as we have a valid calculation self.to_history_button.config(state=NORMAL) self.answer_error.config(text=answer_statement) self.all_calculations_list.append(answer_statement) print(self.all_calculations_list) def clear_entry(self): """Clears the entry box and resets the answer label.""" 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("Distance Converter") Converter() root.mainloop()