from tkinter import * from functools import partial import all_constants as c import conversion_rounding as cr from datetime import date class Converter: """ Weight conversion tool (lb to kg or kg to lb) """ def __init__(self): self.all_calculations_list = [] self.main_frame = Frame(padx=10, pady=10) self.main_frame.grid() self.heading = Label(self.main_frame, text="Weight Converter", font=("Arial", 16, "bold")) self.heading.grid(row=0) instructions = ("Please enter a weight below and then press " "one of the buttons to convert it between " "pounds (lb) and kilograms (kg).") self.instructions = Label(self.main_frame, text=instructions, wraplength=250, width=40, justify="left") self.instructions.grid(row=1) self.entry = Entry(self.main_frame, font=("Arial", 14)) self.entry.grid(row=2, padx=10, pady=10) self.answer_label = Label(self.main_frame, text="Please enter a number", fg="#004C99", font=("Arial", 12, "bold")) self.answer_label.grid(row=3) # Buttons self.button_frame = Frame(self.main_frame) self.button_frame.grid(row=4) button_details_list = [ ["To Kilograms", "#990099", lambda: self.check_weight("lb"), 0, 0], ["To Pounds", "#009900", lambda: self.check_weight("kg"), 0, 1], ["Help / Info", "#CC6600", self.to_help, 1, 0], ["History / Export", "#004C99", self.to_history, 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=12, 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[2] self.to_history_button = self.button_ref_list[3] self.to_history_button.config(state=DISABLED) def check_weight(self, unit): to_convert = self.entry.get() self.answer_label.config(fg="#004C99", font=("Arial", 13, "bold")) self.entry.config(bg="#FFFFFF") error = "Enter a number greater than or equal to 0" has_errors = "no" try: to_convert = float(to_convert) if to_convert >= 0: self.convert(unit, to_convert) else: has_errors = "yes" except ValueError: has_errors = "yes" if has_errors == "yes": self.answer_label.config(text=error, fg="#9C0000", font=("Arial", 10, "bold")) self.entry.config(bg="#F4CCCC") self.entry.delete(0, END) def convert(self, unit, to_convert): if unit == "lb": answer = cr.to_kilogram(to_convert) answer_statement = f"{to_convert} lb is {answer} kg" else: answer = cr.to_pounds(to_convert) answer_statement = f"{to_convert} kg is {answer} lb" self.to_history_button.config(state=NORMAL) self.answer_label.config(text=answer_statement) self.all_calculations_list.append(answer_statement) def to_help(self): DisplayHelp(self) def to_history(self): HistoryExport(self, self.all_calculations_list) class DisplayHelp: def __init__(self, partner): background = "#ffe6cc" self.help_box = Toplevel() 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() Label(self.help_frame, text="Help / Info", font=("Arial", 14, "bold")).grid(row=0) help_text = ("To use the program, enter a weight and choose to convert " "between pounds (lb) and kilograms (kg).\n\n" "Weights must be 0 or higher (no negative values).\n\n" "Use the History / Export button to save your calculations.") Label(self.help_frame, text=help_text, wraplength=350, justify="left").grid(row=1, padx=10) Button(self.help_frame, font=("Arial", 12, "bold"), text="Dismiss", bg="#CC6600", fg="#FFFFFF", command=partial(self.close_help, partner) ).grid(row=2, padx=10, pady=10) for item in [self.help_frame]: item.config(bg=background) def close_help(self, partner): partner.to_help_button.config(state=NORMAL) self.help_box.destroy() class HistoryExport: def __init__(self, partner, calculations): self.history_box = Toplevel() partner.to_history_button.config(state=DISABLED) self.history_box.protocol('WM_DELETE_WINDOW', partial(self.close_history, partner)) if len(calculations) <= c.MAX_CALCS: calc_back = "#D5E8D4" calc_amount = "all your" else: calc_back = "#ffe6cc" calc_amount = f"your recent calculations - showing {c.MAX_CALCS} / {len(calculations)}" recent_intro_txt = f"Below are {calc_amount} calculations." newest_first_list = list(reversed(calculations)) display_list = newest_first_list[:c.MAX_CALCS] display_string = "\n".join(display_list) export_instruction_txt = ("Click Export to save your calculations.\n" "Existing files will be overwritten.") labels = [ ["History / Export", ("Arial", "16", "bold"), None], [recent_intro_txt, ("Arial", "11"), None], [display_string, ("Arial", "14"), calc_back], [export_instruction_txt, ("Arial", "11"), None] ] label_refs = [] for i, item in enumerate(labels): lbl = Label(self.history_box, text=item[0], font=item[1], bg=item[2], wraplength=300, justify="left", pady=10, padx=20) lbl.grid(row=i) label_refs.append(lbl) self.export_filename_label = label_refs[3] btn_frame = Frame(self.history_box) btn_frame.grid(row=4) Button(btn_frame, text="Export", bg="#004C99", fg="white", width=12, command=lambda: self.export_data(calculations) ).grid(row=0, column=0, padx=10, pady=10) Button(btn_frame, text="Close", bg="#666666", fg="white", width=12, command=partial(self.close_history, partner) ).grid(row=0, column=1, padx=10, pady=10) def export_data(self, calculations): today = date.today() file_name = f"weights_{today.strftime('%Y_%m_%d')}" self.export_filename_label.config( fg="#009900", text=f"Export Successful! File: {file_name}.txt", font=("Arial", "12", "bold") ) with open(f"{file_name}.txt", "w") as f: f.write("***** Weight Calculations ******\n") f.write(f"Generated: {today}\n\n") for item in calculations: f.write(item + "\n") def close_history(self, partner): partner.to_history_button.config(state=NORMAL) self.history_box.destroy() # Main if __name__ == "__main__": root = Tk() root.title("Weight Converter") Converter() root.mainloop()