from tkinter import * from datetime import date import all_constants as c import converter as cr from display_help import DisplayHelp from history_export_v2 import HistoryExport class Converter: # Currency conversion tool (NZD to other currencies) def __init__(self): # Currency converter GUI self.all_calculations_list = [] self.curr_frame = Frame(padx=10, pady=10) self.curr_frame.grid() self.curr_heading = Label(self.curr_frame, text="Currency Converter", font=("Arial", "16", "bold")) self.curr_heading.grid(row=0) instructions = ("Please enter an amount in NZD below and " "then press one of the buttons to convert it.") self.curr_instructions = Label(self.curr_frame, text=instructions, wraplength=250, width=40, justify="left") self.curr_instructions.grid(row=1) self.curr_entry = Entry(self.curr_frame, font=("Arial", "14")) self.curr_entry.grid(row=2, padx=10, pady=10) self.answer_error = Label(self.curr_frame, text="Enter an amount to convert", fg="#004C99", font=("Arial", "14", "bold")) self.answer_error.grid(row=3) self.button_frame = Frame(self.curr_frame) self.button_frame.grid(row=4) button_details_list = [ ["To USD", "#990099", lambda: self.check_amount("USD"), 0, 0], ["To EUR", "#009900", lambda: self.check_amount("EUR"), 0, 1], ["To GBP", "#CC0000", lambda: self.check_amount("GBP"), 1, 0], ["To JPY", "#CC6600", lambda: self.check_amount("JPY"), 1, 1], ["To AUD", "#006666", lambda: self.check_amount("AUD"), 2, 0], ["Help / Info", "#666666", self.to_help, 2, 1], ["History / Export", "#004C99", self.to_history, 3, 0], ] self.button_ref_list = [] for item in button_details_list: make_button = Button(self.button_frame, text=item[0], bg=item[1], fg="#FFFFFF", font=("Arial", "12", "bold"), width=12, command=item[2]) make_button.grid(row=item[3], column=item[4], padx=5, pady=5) self.button_ref_list.append(make_button) # history button is index 6 in the list self.to_history_button = self.button_ref_list[6] self.to_history_button.config(state=DISABLED) def check_amount(self, to_currency): # checks the amount is valid and converts or shows error to_convert = self.curr_entry.get() self.answer_error.config(fg="#004C99", font=("Arial", "13", "bold")) self.curr_entry.config(bg="#FFFFFF") error = "Please enter a positive number" has_errors = "no" try: to_convert = float(to_convert) if to_convert > 0: self.convert(to_currency, to_convert) else: has_errors = "yes" except ValueError: has_errors = "yes" if has_errors == "yes": self.answer_error.config(text=error, fg="#9C0000", font=("Arial", "13", "bold")) self.curr_entry.config(bg="#F4CCCC") self.curr_entry.delete(0, END) def convert(self, to_currency, to_convert): # converts NZD to chosen currency and saves to history if to_currency == "USD": answer = cr.to_usd(to_convert) answer_statement = f"NZ${to_convert:.2f} = US${answer}" elif to_currency == "EUR": answer = cr.to_eur(to_convert) answer_statement = f"NZ${to_convert:.2f} = €{answer}" elif to_currency == "GBP": answer = cr.to_gbp(to_convert) answer_statement = f"NZ${to_convert:.2f} = £{answer}" elif to_currency == "JPY": answer = cr.to_jpy(to_convert) answer_statement = f"NZ${to_convert:.2f} = ¥{answer}" else: answer = cr.to_aud(to_convert) answer_statement = f"NZ${to_convert:.2f} = AU${answer}" self.to_history_button.config(state=NORMAL) self.answer_error.config(text=answer_statement) self.all_calculations_list.append(answer_statement) def to_help(self): # opens help window DisplayHelp(self) def to_history(self): # opens history window HistoryExport(self, self.all_calculations_list) # main routine if __name__ == "__main__": root = Tk() root.title("Currency Converter") Converter() root.mainloop()