Build a GUI Real-time Currency Converter using Python (Python Projects)

currency converter in python using tkinter, real time currency converter in python
Here's a code of how you could make a Graphical Real-time Currency Converter using the 'tkinter' library in Python:

real time currency converter in python

import tkinter as tk
import requests

def currency_converter():
    API_KEY = 'YOUR_API_KEY'
    API_URL = f'https://openexchangerates.org/api/latest.json?app_id={API_KEY}'
    
    response = requests.get(API_URL)
    exchange_rates = response.json()['rates']
    
    current_amount = float(current_amount_entry.get())
    current_currency = current_currency_entry.get()
    desired_currency = desired_currency_entry.get()
    
    conversion_rate = exchange_rates[desired_currency] / exchange_rates[current_currency]
    converted_amount = conversion_rate * current_amount
    
    result_label.config(text=f"{current_amount} {current_currency} is equal to {converted_amount} {desired_currency}")

root = tk.Tk()
root.title("Currency Converter")

current_amount_label = tk.Label(root, text="Enter the amount to be converted:")
current_amount_entry = tk.Entry(root)

current_currency_label = tk.Label(root, text="Enter the current currency (e.g. USD, EUR, GBP, INR):")
current_currency_entry = tk.Entry(root)

desired_currency_label = tk.Label(root, text="Enter the desired currency (e.g. USD, EUR, GBP, INR):")
desired_currency_entry = tk.Entry(root)

convert_button = tk.Button(root, text="Convert", command=currency_converter)

result_label = tk.Label(root, text="")

current_amount_label.pack()
current_amount_entry.pack()

current_currency_label.pack()
current_currency_entry.pack()

desired_currency_label.pack()
desired_currency_entry.pack()

convert_button.pack()

result_label.pack()

root.mainloop()


In this code, we have created a GUI with tkinter that takes in the amount to be converted, the current currency, and the desired currency. The exchange rates are fetched from the Open Exchange Rates API and the conversion is done in the 'currency_converter' function. The result is displayed in a label widget in the GUI.
Note: To use the API, you need to sign up for a free API key on the Open Exchange Rates website.
I trust this helps you! if you have any query you can ask me.

Post a Comment