Professional Documents
Culture Documents
Python is a very versatile programming language. Python is being used in almost each
mainstream technology and one can develop literally any application with it. Let’s see a Python
program to convert the currency of one country to that of another country. To use this service,
one must need the API key, which can be get from here. We will use fixer API to get the live
conversion rates and convert the corresponding amount.
Case 1
Modules needed:
1. requests: This module does not build in with python. To install it type the below command in
the terminal or cmd.
pip install requests
Below is the implementation:
Python3
class Currency_convertor:
# empty dict to store the conversion rates
rates = {}
def __init__(self, url):
data = requests.get(url).json()
# Driver code
if __name__ == "__main__":
Input :
From Country: USD
TO Country: INR
Amount: 1
Output :
1 USD = 70.69 INR
Case 2
Modules needed:
2.1: tkinter: It facilities Graphical User interfaces (GUIs).
pip install tkinter
2.2: forex_python: It is a Free Foreign exchange rate and currency conversion library.
pip install forex_python
Implementation:
Python
root.title("Currency converter:GeeksForGeeks")
Tops = Frame(root, bg = '#e6e5e5', pady=2, width=1850, height=100,
relief="ridge")
Tops.grid(row=0, column=0)
variable1 = tk.StringVar(root)
variable2 = tk.StringVar(root)
variable1.set("currency")
variable2.set("currency")
#Function To For Real Time Currency Conversion
def RealTimeCurrencyConversion():
from forex_python.converter import CurrencyRates
c = CurrencyRates()
from_currency = variable1.get()
to_currency = variable2.get()
if (Amount1_field.get() == ""):
tkinter.messagebox.showinfo("Error !!", "Amount Not Entered.\n
Please a valid amount.")
else:
new_amt = c.convert(from_currency, to_currency,
float(Amount1_field.get()))
new_amount = float("{:.4f}".format(new_amt))
Amount2_field.insert(0, str(new_amount))
Amount1_field = tk.Entry(root)
Amount1_field.grid(row=2, column=0, ipadx=28, sticky=E)
Amount2_field = tk.Entry(root)
Amount2_field.grid(row=8, column=0, ipadx=31, sticky=E)
In this tutorial, we are going to build an exciting python project through which
you can convert currencies. For a user interface, we are going to use the tkinter
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
import tkinter as tk
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.
initial_amount = amount
if from_currency != 'USD' :
return amount
Example:
url = 'https://api.exchangerate-api.com/v4/latest/USD'
converter = RealTimeCurrencyConverter(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
#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.intro_label.place(x = 10 , y = 5)
# Entry box
# restricNumberOnly function will restrict thes user to enter invavalid number in Amount field. We will define it later in
code
# dropdown
self.from_currency_variable = StringVar(self)
self.to_currency_variable = StringVar(self)
self.to_currency_variable.set("USD") # default value
self.option_add('*TCombobox*Listbox.font', font)
self.from_currency_dropdown = ttk.Combobox(self,
textvariable=self.from_currency_variable,values=list(self.currency_converter.currencies.keys()), font = font, state =
'readonly', width = 12, justify = tk.CENTER)
self.to_currency_dropdown = ttk.Combobox(self,
textvariable=self.to_currency_variable,values=list(self.currency_converter.currencies.keys()), font = font, state =
'readonly', width = 12, justify = tk.CENTER)
# placing
def perform(self,):
amount = float(self.amount_field.get())
from_curr = self.from_currency_variable.get()
to_curr = self.to_currency_variable.get()
converted_amount= self.currency_converter.convert(from_curr,to_curr,amount)
converted_amount = round(converted_amount, 2)
self.converted_amount_field_label.config(text = str(converted_amount))
regex = re.compile(r"[0-9,]*?(\.)?[0-9,]*$")
result = regex.match(string)
if __name__ == '__main__':
url = 'https://api.exchangerate-api.com/v4/latest/USD'
converter = RealTimeCurrencyConverter(url)
App(converter)
mainloop()
Label_1 = Label(root, font=('lato black', 7, 'bold'), text="", padx=2,
pady=2, bg="#e6e5e5", fg="black")
Label_1.grid(row=9, column=0, sticky=W)
root.mainloop()
Output: