Currency Converter in Python
Prerequisites
The currency converter project in python requires you to have basic
knowledge of python programming and the pygame library.
tkinter – For User Interface (UI)
requests – to get url
To install the tkinter and requests library, type the following code in your
terminal:
pip install tkinter
pip install requests
pip install tkinter pip install requests
pip install tkinter
pip install requests
Download Project Code
Steps to Build the Python Project on
Currency Converter
1. Real-time Exchange rates
2. Import required Libraries
3. CurrencyConverter Class
4. UI for CurrencyConverter
5. Main Function
1. Real-time Exchange rates
To get real-time exchange rates, we will use: https://api.exchangerate-
api.com/v4/latest/USD
Here, we can see the data in JSON format, with the following details:
Base – USD: It means we have our base currency USD. which means to
convert any currency we have to first convert it to USD then from USD, we
will convert it in whichever currency we want.
Date and time: It shows the last updated date and time.
Rates: It is the exchange rate of currencies with base currency USD.
2. Import the libraries:
For this project based on Python, we are using the tkinter and requests
library. So we need to import the library.
import requests
from tkinter import *
import tkinter as tk
from tkinter import ttk
import requests from tkinter import * import tkinter as tk from tkinter
import ttk
import requests
from tkinter import *
import tkinter as tk
from tkinter import ttk
3. Create the CurrencyConverter class:
Now we will create the CurrencyConverter class which will get the real time
exchange rate and convert the currency and return the converted amount.
3.1. Let’s create the constructor of class.
class CurrencyConverter():
def __init__(self,url):
self.data= requests.get(url).json()
self.currencies = self.data['rates']
class CurrencyConverter(): def __init__(self,url): self.data=
requests.get(url).json() self.currencies = self.data['rates']
class CurrencyConverter():
def __init__(self,url):
self.data= requests.get(url).json()
self.currencies = self.data['rates']
requests.get(url) load the page in our python program and then .json() will
convert the page into the json file. We store it in a data variable.
3.2. Convert() method:
def convert(self, from_currency, to_currency, amount):
initial_amount = amount
#first convert it into USD if it is not in USD.
# because our base currency is USD
if from_currency != 'USD' :
amount = amount / self.currencies[from_currency]
# limiting the precision to 4 decimal places
amount = round(amount * self.currencies[to_currency], 4)
return amount
def convert(self, from_currency, to_currency, amount): initial_amount =
amount #first convert it into USD if it is not in USD. # because our base
currency is USD if from_currency != 'USD' : amount = amount /
self.currencies[from_currency] # limiting the precision to 4 decimal places
amount = round(amount * self.currencies[to_currency], 4) return amount
def convert(self, from_currency, to_currency, amount):
initial_amount = amount
#first convert it into USD if it is not in USD.
# because our base currency is USD
if from_currency != 'USD' :
amount = amount / self.currencies[from_currency]
# limiting the precision to 4 decimal places
amount = round(amount * self.currencies[to_currency], 4)
return amount
This method takes following arguments:
From_currency: currency from which you want to convert.
to _currency: currency in which you want to convert.
Amount: how much amount you want to convert.
And returns the converted amount.
Example:
url = 'https://api.exchangerate-api.com/v4/latest/USD'
converter = CurrencyConverter(url)
print(converter.convert('INR','USD',100))
url = 'https://api.exchangerate-api.com/v4/latest/USD' converter =
CurrencyConverter(url) print(converter.convert('INR','USD',100))
url = 'https://api.exchangerate-api.com/v4/latest/USD'
converter = CurrencyConverter(url)
print(converter.convert('INR','USD',100))
OUTPUT: 1.33
100 Indian rupees = 1.33 US dollars
4. Now let’s create a UI for the currency converter
To Create UI we will create a class CurrencyConverterUI
def __init__(self, converter):
tk.Tk.__init__(self)
self.title = 'Currency Converter'
self.currency_converter = converter
def __init__(self, converter): tk.Tk.__init__(self) self.title = 'Currency
Converter' self.currency_converter = converter
def __init__(self, converter):
tk.Tk.__init__(self)
self.title = 'Currency Converter'
self.currency_converter = converter
Converter: Currency Converter object which we will use to convert
currencies. Above code will create a Frame.
Let’s Create the Converter
self.geometry("500x200")
#Label
self.intro_label = Label(self, text = 'Welcome to Real Time Currency
Convertor', fg = 'blue', relief = tk.RAISED, borderwidth = 3)
self.intro_label.config(font = ('Courier',15,'bold'))
self.date_label = Label(self, text = f"1 Indian Rupee equals =
{self.currency_converter.convert('INR','USD',1)} USD \n Date :
{self.currency_converter.data['date']}", relief = tk.GROOVE, borderwidth
= 5)
self.intro_label.place(x = 10 , y = 5)
self.date_label.place(x = 170, y= 50)
self.geometry("500x200") #Label self.intro_label = Label(self, text =
'Welcome to Real Time Currency Convertor', fg = 'blue', relief = tk.RAISED,
borderwidth = 3) self.intro_label.config(font = ('Courier',15,'bold'))
self.date_label = Label(self, text = f"1 Indian Rupee equals =
{self.currency_converter.convert('INR','USD',1)} USD \n Date :
{self.currency_converter.data['date']}", relief = tk.GROOVE, borderwidth =
5) self.intro_label.place(x = 10 , y = 5) self.date_label.place(x = 170, y= 50)
self.geometry("500x200")
#Label
self.intro_label = Label(self, text = 'Welcome to Real Time Currency
Convertor', fg = 'blue', relief = tk.RAISED, borderwidth = 3)
self.intro_label.config(font = ('Courier',15,'bold'))
self.date_label = Label(self, text = f"1 Indian Rupee equals =
{self.currency_converter.convert('INR','USD',1)} USD \n Date :
{self.currency_converter.data['date']}", relief = tk.GROOVE,
borderwidth = 5)
self.intro_label.place(x = 10 , y = 5)
self.date_label.place(x = 170, y= 50)