from tkinter import * from functools import partial import all_constants as c import conversion_rounding as cr from datetime import date 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, select the units " "to convert from and to, then press Convert.") self.dist_instructions = Label(self.dist_frame, text=instructions, wraplength=300, 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) # --- Answer / error label --- self.answer_error = Label(self.dist_frame, text="Please enter a number", fg="#9C0000", font=("Arial", "13", "bold")) self.answer_error.grid(row=4, columnspan=2) # --- Buttons --- self.button_frame = Frame(self.dist_frame) self.button_frame.grid(row=5) button_details_list = [ ["Convert", "#009900", self.check_distance, 0, 0], ["Help / Info", "#CC6600", self.to_help, 0, 1], ["History / Export","#004C99", self.to_history, 1, 0], ["Clear", "#666666", self.clear_entry, 1, 1], ] self.button_ref_list = [] for item in button_details_list: btn = Button(self.button_frame, text=item[0], bg=item[1], fg="#FFFFFF", font=("Arial", "12", "bold"), width=14, command=item[2]) btn.grid(row=item[3], column=item[4], padx=5, pady=5) self.button_ref_list.append(btn) self.to_help_button = self.button_ref_list[1] self.to_history_button = self.button_ref_list[2] self.to_history_button.config(state=DISABLED) def check_distance(self): """ Validates input and kicks off conversion, or shows a custom error. """ to_convert = self.dist_entry.get() from_unit = self.from_unit_var.get() to_unit = self.to_unit_var.get() # Reset styles self.answer_error.config(fg="#004C99", font=("Arial", "13", "bold")) self.dist_entry.config(bg="#FFFFFF") has_errors = "no" error = "Please enter a valid positive number" try: value = float(to_convert) if value < 0: error = "Please enter a number greater than or equal to 0" has_errors = "yes" elif from_unit == to_unit: error = "Please choose two different units to convert between" has_errors = "yes" else: self.convert(value, from_unit, to_unit) except ValueError: has_errors = "yes" 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, value, from_unit, to_unit): """ Performs the conversion, updates the answer label, and records the calculation for History / Export. """ result = cr.convert_distance(value, from_unit, to_unit) answer_statement = f"{value} {from_unit} = {result} {to_unit}" self.answer_error.config(text=answer_statement, fg="#004C99") # Enable history button once we have at least one calculation self.to_history_button.config(state=NORMAL) 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")) def to_help(self): DisplayHelp(self) def to_history(self): """ Opens history dialogue box and disables history button (so users can't create multiple history boxes). """ HistoryExport(self, self.all_calculations_list) class DisplayHelp: def __init__(self, partner): background = "#ffe6cc" self.help_box = Toplevel() self.help_box.title("Help / Info") partner.to_help_button.config(state=DISABLED) self.help_box.protocol('WM_DELETE_WINDOW', partial(self.close_help, partner)) self.help_frame = Frame(self.help_box, width=300, height=200) self.help_frame.grid() self.help_heading_label = Label(self.help_frame, text="Help / Info", font=("Arial", "14", "bold")) self.help_heading_label.grid(row=0) help_text = ("To use the program, simply enter the distance you " "wish to convert, then select the unit you are " "converting FROM (next to the entry box) and the " "unit you are converting TO using the second " "dropdown.\n\n" "Supported units: km (kilometres), m (metres), " "cm (centimetres), and mm (millimetres).\n\n" "Press Convert to see the result. To see your " "calculation history and export it to a text file, " "click the 'History / Export' button.") self.help_text_label = Label(self.help_frame, text=help_text, wraplength=350, justify="left") self.help_text_label.grid(row=1, padx=10) self.dismiss_button = Button(self.help_frame, font=("Arial", "12", "bold"), text="Dismiss", bg="#CC6600", fg="#FFFFFF", command=partial(self.close_help, partner)) self.dismiss_button.grid(row=2, padx=10, pady=10) recolour_list = [self.help_frame, self.help_heading_label, self.help_text_label] for item in recolour_list: item.config(bg=background) def close_help(self, partner): partner.to_help_button.config(state=NORMAL) self.help_box.destroy() class HistoryExport: """ Displays history dialogue box with export functionality. """ def __init__(self, partner, calculations): self.history_box = Toplevel() self.history_box.title("History / Export") partner.to_history_button.config(state=DISABLED) self.history_box.protocol('WM_DELETE_WINDOW', partial(self.close_history, partner)) self.history_frame = Frame(self.history_box) self.history_frame.grid() if len(calculations) <= c.MAX_CALCS: calc_back = "#D5E804" calc_amount = "all your" else: calc_back = "#ffe6cc" calc_amount = (f"your recent calculations - " f"showing {c.MAX_CALCS} / {len(calculations)}") recent_intro_txt = (f"Below are {calc_amount} calculations.") # Build display string (newest first) newest_first_list = list(reversed(calculations)) newest_first_string = "" if len(newest_first_list) <= c.MAX_CALCS: for item in newest_first_list[:-1]: newest_first_string += item + "\n" newest_first_string += newest_first_list[-1] else: for item in newest_first_list[:c.MAX_CALCS - 1]: newest_first_string += item + "\n" newest_first_string += newest_first_list[c.MAX_CALCS - 1] export_instruction_txt = ("Press to save your calculations " "to a text file. If the file already exists " "it will be overwritten.") history_labels_list = [ ["History / Export", ("Arial", "16", "bold"), None], [recent_intro_txt, ("Arial", "11"), None], [newest_first_string, ("Arial", "14"), calc_back], [export_instruction_txt,("Arial", "11"), None], ] history_label_ref = [] for count, item in enumerate(history_labels_list): make_label = Label(self.history_box, text=item[0], font=item[1], bg=item[2], wraplength=300, justify="left", pady=10, padx=20) make_label.grid(row=count) history_label_ref.append(make_label) self.export_filename_label = history_label_ref[3] self.hist_button_frame = Frame(self.history_box) self.hist_button_frame.grid(row=4) button_details_list = [ ["Export", "#004C99", lambda: self.export_history(calculations), 0, 0], ["Close", "#666666", partial(self.close_history, partner), 0, 1], ] for btn in button_details_list: self.make_button = Button(self.hist_button_frame, font=("Arial", "12", "bold"), text=btn[0], bg=btn[1], fg="#FFFFFF", width=12, command=btn[2]) self.make_button.grid(row=btn[3], column=btn[4], padx=10, pady=10) def export_history(self, calculations): today = date.today() day = today.strftime("%d") month = today.strftime("%m") year = today.strftime("%Y") file_name = f"distances_{day}_{month}_{year}" write_to = f"{file_name}.txt" with open(write_to, 'w') as text_file: text_file.write("***** Distance Calculations *****\n") text_file.write(f"Generated: {day}/{month}/{year}\n\n") text_file.write("Here is your calculation history " "(oldest to newest)...\n") for item in calculations: text_file.write(item + "\n") success_string = (f"Export successful! The file is called " f"{file_name}.txt") self.export_filename_label.config(bg="#009900", text=success_string, font=("Arial", "12", "bold")) def close_history(self, partner): partner.to_history_button.config(state=NORMAL) self.history_box.destroy() # main routine if __name__ == "__main__": root = Tk() root.title("Distance Converter") Converter() root.mainloop()