Currency Converter using python

Currency Converter using python

Introduction

We will demonstrate how to create a Currency Converter using python. For this project, the Tkinter library and forex_python is utilized. Tkinter is a powerful tool for developing graphical user interfaces (GUIs). The forex-python library provides a convenient way to fetch foreign exchange rates and perform currency conversions in Python. This program has straightforward labels, entry, option menu, button and grid layout manager.

Explanation

A basic Tkinter-based GUI with labels, entry, option menu, buttons are produced by this code. Currency Converter will feature a user-friendly interface with a labels, entry, option menu, bottons. The users may convert the currency using ‘convert’ button. The user can enter the currency in entry then select the targeted currency and can convert using convert button. Users can select the source currency (“From”) and the target currency (“To”) from dropdown menus. The program fetches real-time exchange rates using the forex-python library to ensure accurate currency conversion.

After conversion, the program displays the result of the conversion in the GUI, showing the converted amount and the selected currencies. The currency converter supports a variety of currencies including USD, EUR, JPY, GBP, CAD, AUD, CHF, CNY, and INR, allowing users to convert between different currencies as needed. The program includes basic error handling to handle invalid input or connectivity issues gracefully, providing informative error messages to users when necessary.

Source code for currency converter

import tkinter as tk
from forex_python.converter import CurrencyRates

class CurrencyConverter:
    def __init__(self, master):
        self.master = master
        self.master.title("Currency Converter")
        
        self.c = CurrencyRates()
        
        self.amount_label = tk.Label(master, text="Amount:")
        self.amount_label.grid(row=0, column=0, padx=10, pady=10)
        self.amount_entry = tk.Entry(master)
        self.amount_entry.grid(row=0, column=1, padx=10, pady=10)
        
        self.from_label = tk.Label(master, text="From:")
        self.from_label.grid(row=1, column=0, padx=10, pady=10)
        self.from_currency = tk.StringVar()
        self.from_currency.set("USD")
        self.from_menu = tk.OptionMenu(master, self.from_currency, "USD", "EUR", "JPY", "GBP", "CAD", "AUD", "CHF", "CNY", "INR")
        self.from_menu.grid(row=1, column=1, padx=10, pady=10)
        
        self.to_label = tk.Label(master, text="To:")
        self.to_label.grid(row=2, column=0, padx=10, pady=10)
        self.to_currency = tk.StringVar()
        self.to_currency.set("EUR")
        self.to_menu = tk.OptionMenu(master, self.to_currency, "USD", "EUR", "JPY", "GBP", "CAD", "AUD", "CHF", "CNY", "INR")
        self.to_menu.grid(row=2, column=1, padx=10, pady=10)
        
        self.convert_button = tk.Button(master, text="Convert", command=self.convert)
        self.convert_button.grid(row=3, columnspan=2, padx=10, pady=10)
        
        self.result_label = tk.Label(master, text="")
        self.result_label.grid(row=4, columnspan=2, padx=10, pady=10)
        
    def convert(self):
        amount = float(self.amount_entry.get())
        from_currency = self.from_currency.get()
        to_currency = self.to_currency.get()
        result = self.c.convert(from_currency, to_currency, amount)
        self.result_label.config(text=f"{amount} {from_currency} = {result} {to_currency}")

def main():
    root = tk.Tk()
    converter = CurrencyConverter(root)
    root.mainloop()

if __name__ == "__main__":
    main()

Output

currency converter using python

Leave a Comment