Real time currency converter using Python Tkinter


In today's globalized world, cash transformation plays an imperative part in different monetary exchanges and worldwide exchanges. Whether you're arranging a trip overseas, overseeing outside ventures, or running multinational commerce, having to get to real-time money change is basic. In this article, we are going investigate how to construct a real-time money converter utilizing Python Tkinter, a prevalent GUI toolkit. By leveraging the power of Python and Tkinter, ready to make an instinctive and user-friendly application that not as it were performs exact cash transformations but to gives an intuitive interface for clients to input their craved sums and select the base and target monetary standards. Through the article, we are going dig into the fundamental calculations, and step-by-step usage, and show distinctive approaches to cater to different needs. 

Currency Converter using Python Tkinter

Real-time cash transformation utilizing Python Tkinter includes making a graphical client interface (GUI) application that permits clients to change over monetary standards on-the-fly. The application takes after a step-by-step preparation, beginning with the client entering a sum to be changed over and selecting the base and target monetary standards. To bring real-time trade rates, the application is interatomic with a money trade rate API. This may be done utilizing the 'requests' library, which permits making HTTP demands to the API endpoint. The API returns a JSON reaction containing the most recent trade rates for different monetary forms. The application extricates the change rate between the chosen base and target monetary standards from the API reaction.

Approach

  • Approach 1 − Using the Web API for Currency Conversion

  • Approach 2 − Using a Pre-loaded Currency Conversion Table

  • Approach 3 − Using a python Library For Currency Conversion

Approach 1: Using the Web API for Currency Conversion

The primary approach includes employing a web API to get real-time currency trade rates and perform the change. Able to use well-known APIs such as Open Trade Rates or Exchange Rate-API to urge the most recent trade rates. Here are the steps to execute this approach −

Algorithm

  • Step 1 − Introduce the required bundles.

  • Step 2 − Creation of a function convert_currency().

  • Step 3 − Invoke the get() method to obtain the amount, base_currency and target _currency from the user.

  • Step 4 − Get real-time trade rates.

  • Step 5 − Set the labels on the window and at last run the application.

Example

import tkinter as tk
import requests

def convert_currency():
    amount = float(entry.get())
    base_currency = base_currency_var.get()
    target_currency = target_currency_var.get()

    response = requests.get(f"https://api.exchangerate-api.com/v4/latest/{base_currency}")
    data = response.json()

    conversion_rate = data['rates'][target_currency]
    converted_amount = amount * conversion_rate

    result_label.config(text=str(converted_amount))

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

label1 = tk.Label(window, text="Amount:")
label1.pack()

entry = tk.Entry(window)
entry.pack()

label2 = tk.Label(window, text="Base Currency:")
label2.pack()

base_currency_var = tk.StringVar(window)
base_currency_var.set("USD")  # Default base currency

base_currency_dropdown = tk.OptionMenu(window, base_currency_var, "USD", "EUR", "GBP", "INR")
base_currency_dropdown.pack()

label3 = tk.Label(window, text="Target Currency:")
label3.pack()

target_currency_var = tk.StringVar(window)
target_currency_var.set("EUR")  # Default target currency

target_currency_dropdown = tk.OptionMenu(window, target_currency_var, "USD", "EUR", "GBP", "INR")
target_currency_dropdown.pack()

label4 = tk.Label(window, text="Converted Amount:")
label4.pack()

result_label = tk.Label(window, text="")
result_label.pack()

button = tk.Button(window, text="Convert", command=convert_currency)
button.pack()

window.mainloop()

Output

After running the application, a GUI window will appear with input fields for the amount, labels for the converted amount, and a "Convert" button. Users can enter the amount in the input field and click the "Convert" button to see the converted amount in the result label.

Approach 2: Using Preload-ed Currency Conversion Table

In a few scenarios, getting to a web API might not be attainable. In such cases, we are able to utilize a preloaded cash transformation table. Here are the steps for this approach −

Multiple steps need to require to run the program code are listed below −

  • Step 1 − Import the tkinter module. Make a money transformation table.

  • Step 2 − Moment required libraries and perused the change table.

  • Step 3 − Define the function named convert_currency() and write the necessary code and condition to obtain the current from the user.

  • Step 4 − Run the application.

Example

import tkinter as tk

conversion_table = {
    "USD": {
        "EUR": 0.85,
        "GBP": 0.72,
        "JPY": 109.71,
        "CAD": 1.22,
        "AUD": 1.32
    },
    "EUR": {
        "USD": 1.18,
        "GBP": 0.85,
        "JPY": 129.67,
        "CAD": 1.47,
        "AUD": 1.59
    },
    "GBP": {
        "USD": 1.39,
        "EUR": 1.18,
        "JPY": 151.37,
        "CAD": 1.70,
        "AUD": 1.84
    },
    "JPY": {
        "USD": 0.0091,
        "EUR": 0.0077,
        "GBP": 0.0066,
        "CAD": 0.011,
        "AUD": 0.012
    },
    "CAD": {
        "USD": 0.82,
        "EUR": 0.68,
        "GBP": 0.59,
        "JPY": 87.47,
        "AUD": 1.08
    },
    "AUD": {
        "USD": 0.76,
        "EUR": 0.63,
        "GBP": 0.54,
        "JPY": 81.75,
        "CAD": 0.93
    }
}

def convert_currency():
    amount = float(entry.get())
    base_currency = base_currency_var.get()
    target_currency = target_currency_var.get()

    if base_currency == target_currency:
        converted_amount = amount
    else:
        conversion_rate = conversion_table[base_currency][target_currency]
        converted_amount = amount * conversion_rate

    result_label.config(text=str(converted_amount))

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

label1 = tk.Label(window, text="Amount:")
label1.pack()

entry = tk.Entry(window)
entry.pack()

label2 = tk.Label(window, text="Base Currency:")
label2.pack()

base_currency_var = tk.StringVar()
base_currency_var.set("USD")

base_currency_menu=tk.OptionMenu(window,base_currency_var,*conversion_table.keys())
base_currency_menu.pack()

label3 = tk.Label(window, text="Target Currency:")
label3.pack()

target_currency_var = tk.StringVar()
target_currency_var.set("EUR")

target_currency_menu=tk.OptionMenu(window,target_currency_var,*conversion_table.keys())
target_currency_menu.pack()

label4 = tk.Label(window, text="Converted Amount:")
label4.pack()

result_label = tk.Label(window, text="")
result_label.pack()

button = tk.Button(window, text="Convert", command=convert_currency)
button.pack()

window.mainloop()

Output

Conclusion

In Conclusion, we investigated how to build a real-time cash converter utilizing Python Tkinter. We examined three distinctive approaches, counting utilizing a web API, a pre-loaded money transformation table, and a Python library for money change. Each approach has its preferences and can be chosen based on particular prerequisites. By taking after the step-by-step execution and understanding the calculations included, users make a money converter application and perform real-time money changes with ease.

Updated on: 04-Sep-2023

526 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements