Currency Converter in Python Project Web
Currency Converter in Python Project Web
Prerequisites
The currency converter project in python requires you to have basic
knowledge of python programming and the pygame library.
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
import tkinter as tk
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
if from_currency != 'USD' :
return 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
tk.Tk.__init__(self)
self.currency_converter = converter
self.geometry("500x200")
#Label
self.intro_label.config(font = ('Courier',15,'bold'))
self.intro_label.place(x = 10 , y = 5)