0% found this document useful (0 votes)
211 views

Currency Converter in Python Project Web

This document describes how to build a currency converter in Python. It involves: 1. Importing required libraries like tkinter and requests. 2. Creating a CurrencyConverter class to get real-time exchange rates from an API and define a convert method. 3. Building a graphical user interface (GUI) using tkinter to allow users to select currencies and amounts to convert. The converter works by first converting unknown currencies to USD as the base currency, then performing the conversion to the desired currency. Real-time exchange rates are retrieved from an API to ensure accurate conversions.

Uploaded by

You
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
211 views

Currency Converter in Python Project Web

This document describes how to build a currency converter in Python. It involves: 1. Importing required libraries like tkinter and requests. 2. Creating a CurrencyConverter class to get real-time exchange rates from an API and define a convert method. 3. Building a graphical user interface (GUI) using tkinter to allow users to select currencies and amounts to convert. The converter works by first converting unknown currencies to USD as the base currency, then performing the conversion to the desired currency. Real-time exchange rates are retrieved from an API to ensure accurate conversions.

Uploaded by

You
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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)

You might also like