import tkinter as tk from tkinter import ttk from tkinter import messagebox # Updated exchange rates for 2024 (example values) exchange_rates = {'NZD': {'USD': 0.60, 'CAD': 0.81}, 'USD': {'NZD': 1.67, 'CAD': 1.36}, 'CAD': {'NZD': 1.23, 'USD': 0.74}} # Button colors BUTTON_COLOR = "#fefae0" HELP_BUTTON_COLOR = "#fefae0" #Input Box Colours INPUT_BOX_COLOUR = "#fefae0" class CurrencyConverterApp: def __init__(self, root): self.root = root self.root.title("Currency Converter") self.root.configure(bg="#dda15e") # Set background color for the entire window # Create Frames self.input_frame = tk.Frame(root, padx=10, pady=10, bg="#dda15e") self.input_frame.grid(row=0, column=0) self.result_frame = tk.Frame(root, padx=10, pady=10, bg="#dda15e") self.result_frame.grid(row=1, column=0) # Labels and Entry self.from_currency_label = tk.Label(self.input_frame, text="From Currency:", fg="#361d18", font=("Arial", 12, "bold"), bg="#dda15e") self.from_currency_label.grid(row=0, column=0, padx=5, pady=5) self.from_currency_var = tk.StringVar() self.from_currency_menu = ttk.Combobox(self.input_frame, textvariable=self.from_currency_var, values=list(exchange_rates.keys()), style="My.TCombobox") self.from_currency_menu.grid(row=0, column=1, padx=5, pady=5) self.from_currency_var.set("NZD") self.to_currency_label = tk.Label(self.input_frame, text="To Currency:", fg="#361d18", font=("Arial", 12, "bold"), bg="#dda15e") self.to_currency_label.grid(row=1, column=0, padx=5, pady=5) self.to_currency_var = tk.StringVar() self.to_currency_menu = ttk.Combobox(self.input_frame, textvariable=self.to_currency_var, values=list(exchange_rates.keys()), style="My.TCombobox") self.to_currency_menu.grid(row=1, column=1, padx=5, pady=5) self.to_currency_var.set("USD") self.amount_label = tk.Label(self.input_frame, text="Amount:", bg="#dda15e", font=("Arial", 12, "bold")) self.amount_label.grid(row=2, column=0, padx=5, pady=5) self.amount_entry = tk.Entry(self.input_frame, bg="#fefae0") self.amount_entry.grid(row=2, column=1, padx=5, pady=5) # Result Label self.result_label = tk.Label(self.result_frame, text="", padx=10, pady=10, fg="#361d18", font=("Arial", 16, "bold"), bg="#dda15e") self.result_label.pack() # Convert Button self.convert_button = tk.Button(self.input_frame, text="Convert", command=self.convert_currency, bg=BUTTON_COLOR, fg="#361d18", font=("Arial", 12, "bold")) self.convert_button.grid(row=3, column=0, columnspan=2, padx=5, pady=5) # History Button self.history_button = tk.Button(self.root, text="History", command=self.display_history, bg=BUTTON_COLOR, fg="#361d18", font=("Arial", 12, "bold")) self.history_button.grid(row=3, column=0, padx=5, pady=(0, 5), sticky='ew') # Initialize history list self.history = [] def convert_currency(self): from_currency = self.from_currency_var.get() to_currency = self.to_currency_var.get() try: amount = float(self.amount_entry.get()) except ValueError: messagebox.showerror("Error", "Please enter a valid amount.") return if from_currency == to_currency: messagebox.showerror("Error", "Please select different currencies.") return converted_amount = amount * exchange_rates[from_currency][to_currency] self.result_label.config(text=f"{amount} {from_currency} equals {converted_amount:.2f} {to_currency}") # Append the conversion result to the history list history_entry = f"{amount} {from_currency} equals {converted_amount:.2f} {to_currency}" self.history.append(history_entry) # Update the history file with open("conversion_history.txt", "a") as file: file.write(history_entry + "\n") def display_history(self): self.root.withdraw() # Hide the main window history_window = tk.Toplevel(self.root) history_window.title("Conversion History") history_window.geometry("300x500") history_window.configure(bg="#bc6c25") # Create a Text widget to display history entries history_text = "\n".join(self.history) history_text_widget = tk.Text(history_window, padx=10, pady=10, fg="#361d18", font=("Arial", 12), bg="#fefae0", wrap="word") history_text_widget.insert(tk.END, history_text) history_text_widget.config(state=tk.DISABLED) history_text_widget.pack(expand=True, fill=tk.BOTH) # Add a button to add history to a text file add_to_file_button = tk.Button(history_window, text="Add to File", command=self.add_to_file, bg=BUTTON_COLOR, fg="#361d18", font=("Arial", 12, "bold")) add_to_file_button.pack(side=tk.BOTTOM, padx=20, pady=(0, 10), fill=tk.X) # Add a back button back_button = tk.Button(history_window, text="Back", command=lambda: self.close_history(history_window), bg="#fefae0", fg="#361d18", font=("Arial", 12, "bold")) back_button.pack(side=tk.BOTTOM, padx=20, pady=10, fill=tk.X) def add_to_file(self): with open("conversion_history.txt", "a") as file: for entry in self.history: file.write(entry + "\n") messagebox.showinfo("File Updated", "Conversion history has been added to the file.") def close_history(self, history_window): self.root.deiconify() # Show the main window history_window.destroy() # Close the history window root = tk.Tk() app = CurrencyConverterApp(root) root.mainloop()