Real time currency conversion GUI application in Python using fixer io

In this tutorial, we will learn how to build a real time currency conversion GUI application in Python using Fixer.io API and Tkinter.

Python is an all-rounder language that one can use for a wide range of tasks. One such task is that of real-time currency conversion. It becomes necessary to know the value of your home currency in different countries not only to understand the county’s economy but also for easy international monetary transactions.  Exchange rates are very important aspects in determining the value of one currency in terms of another.

So, what exactly happens in the currency conversion?
When a user enters an amount and then specifies the currency in which they want to know the value, the convertor calculates and displays the same.
You can develop such an application using python’s fixer.io API. Further, you can create a graphical user interface for the same, also using Python.
Let us learn the same now!

The Fixer API for Python

Fixer is an easy-to-use, lightweight API that provides foreign exchange rates( both current and old). The exchange rates here are updated regularly and are in accordance with the European Central Bank.

Now, let us look into the procedure for getting the information from the fixer and further performing the conversion.

Procedure

First of all, install the requests module that allows you to send HTTP requests by typing the following command on your terminal.

pip install requests

Before proceeding into the program,  you must first sign up on the fixer’s official website in order to use it. There are different free and paid plans from which you can choose accordingly. You then get an API access key which is your personal password for the fixer API.
To do the same, go to https://fixer.io/product

Once you satisfy the above requirements, you can proceed to code.

import requests
class currency_conversion:
  rates = {}
  def __init__(self, url):
    data = requests.get(url).json()
    self.rates = data["rates"]
  def convert(self, from_currency, to_currency, amount):
    initial_amount = amount
    if from_currency != 'EUR' :
      amount = amount / self.rates[from_currency]
    print('{} {} = {} {}'.format(initial_amount, from_currency, amount, to_currency))
if __name__ == "__main__":
  url = str.__add__('http://data.fixer.io/api/latest?access_key=','YOUR API ACCESS KEY')
  c = currency_conversion(url)
  from_country = input("From Country: ")
  to_country = input("TO Country: ")
  amount = int(input("Amount: "))
  c.convert(from_country, to_country, amount)
From Country: INR
TO Country: AUS
Amount: 1
1 INR = 0.011833479839702735 AUS

The above is a simple program for currency conversion using fixer.io. In the program,

  • We define the main function, which acts as the driver for the program.
    Here, we along with taking necessary inputs, also send an HTTP request to the fixer API, in which we specify the private API access key.
  • Fixer returns foreign exchange rates in standard JSON format along with the time at which it was collected, date of the same, base object(base code containing currency code of base currency) etc. The conversion rates are what we need to perform our currency conversion. So, we extract just the conversion rates from the JSON data that is got.
  • Further, we define a function for getting the value after conversion. The conversion is based on simple cross-multiplication between the amount and conversion rates. The standard base currency is EUR, which is why the if statement goes as shown. Once the conversion is done, the results are printed as shown.

Fun Fact:
The fixer API provides you with rates of around 170 world currencies. Also, it refreshes the rates every hour.

Also read, Free Currency Converter PHP using Fixer io APIA

Creating a Graphical User Interface for our Currency Converter in Python

Python provides various options for building GUI’s, among which Tkinter is one of the simplest. It provides a simple yet efficient way for building GUI’s using Python.

Now, we create a GUI for the currency conversion using the Tkinter module.

from tkinter import *
import requests
from tkinter import messagebox 
def clearAll() : 
    fromField.delete(0, END) 
    toField.delete(0, END) 
    amountField.delete(0, END) 
    finField.delete(0, END) 
def checkError() : 
    if (fromField.get() == "" or toField.get() == "" or amountField.get() == "") : 
        messagebox.showerror("Input Error") 
        clearAll() 
        return
def prior():
    from_currency = (fromField.get()) 
    to_currency = (toField.get()) 
    amount = int(amountField.get())
    currency_conversion(from_currency,to_currency,amount)    
def currency_conversion():
    from_currency = (fromField.get()) 
    to_currency = (toField.get()) 
    amount = int(amountField.get())
    response=requests.get(url)            
    rate=response.json()['rates'][from_currency]
    amount_in_EUR=amount/rate
    amount=amount_in_EUR*(response.json()['rates'][to_currency])
    finField.insert(10, str(amount)) 
if __name__ == "__main__" : 
    url = str.__add__('http://data.fixer.io/api/latest?access_key=', '7cdf410b0b856588f3c1f803302d1834')  
    gui = Tk() 
    gui.configure(background = "turquoise") 
    gui.title("Currency Convertor") 
    gui.geometry("525x300") 
    dob = Label(gui, text = "Enter Details", bg = "red") 
    from_label = Label(gui, text = "from", bg = "turquoise")
    to_label = Label(gui, text = "To", bg = "turquoise")
    amount_label = Label(gui, text = "Amount", bg = "turquoise")
    convAmt = Label(gui, text = "Converted Amount", bg = "turquoise")
    convert = Button(gui, text = "Convert", fg = "white", bg = "black", command =currency_conversion) 
    clearAllEntry = Button(gui, text = "Clear All", fg = "white", bg = "black", command = clearAll) 
    fromField= Entry(gui)
    toField= Entry(gui)
    amountField= Entry(gui)
    finField=Entry(gui)
    dob.grid(row = 0, column = 1) 
    from_label.grid(row = 1, column = 0)
    fromField.grid(row = 1, column = 1)
    to_label.grid(row = 2, column = 0)
    toField.grid(row = 2, column = 1)
    amount_label.grid(row = 3, column = 0)
    amountField.grid(row = 3, column = 1)
    convert.grid(row = 4, column = 2)
    convAmt.grid(row = 9, column = 2)
    finField.grid(row = 10, column = 2)
    clearAllEntry.grid(row = 12, column = 2) 
    gui.mainloop()

Tkinter provides a basic interface, whose configurations like background, colors, position, size, etc can be set. You can also define various labels, form fields and buttons.
In the above code, we have defined a basic GUI using Tkinter and linked its code with the logic for currency conversion.
This creates a GUI as shown:
Real time currency conversion GUI application in Python using fixer io

Real time currency conversion GUI application in Python using fixer io

To understand each of the features of Tkinter that have been used, you can go through;
Tkinter messagebox in Python,
How to pass arguments to a Button command in Tkinter,
Add padding to a Tkinter widget only on one side in Python

Leave a Reply

Your email address will not be published.