The Currency Converter In Python is of practical use to tourists who travel abroad, to businesses that do business overseas or are involved in imports and exports, and to FX traders.
A universal currency converter is an app or web tool that allows for the quick conversion of any currency into any other currency.
What is the process of Converting Currency?
Currency conversion rates can be done manually or through an online currency exchange.
You must first check the currency rate using an Internet exchange rate calculator or by contacting your bank.
Currency Conversion Code in Python: Prerequisites
The Real-Time Currency Converter using 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
Project Details and Technology
Project Name: | Currency Converter In Python Project With Source Code |
Abstract: | Currency Converter Program in Python is an app or web tool that allows for the quick conversion of any currency into any other currency. |
Language/s Used: | Python (GUI Based) |
Python version (Recommended): | 3.8 or 3.9 |
Type: | Desktop Application |
Developer: | Source Code Hero |
Updates: | 0 |
By the way, if you are new to Python programming and don’t have any idea what Python IDE to use, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS for you.
Additionally, I also have here How to Download and Install the Latest Version of Python on Windows.
To start executing a Currency Converter In Python With Source Code, make sure that you have installed Python on your computer.
Steps on how to build The Currency Converter API Python
# | Currency Converter API Python |
1. | Real-time Exchange rates |
2. | Import required Libraries |
3. | CurrencConverter Class |
4. | UI for CurrencyConverter |
5. | Main Function |
Time needed: 5 minutes
These are the steps on How To Build The Currency Converter API Python.
- Real-time Exchange rates
To get real-time exchange rates, we will use: https://api.exchangerate-api.com/v4/latest/USD
- Import the Libraries
For this project based on Python, we are using the tkinter and requests library. So we need to import the library.
- 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.
- Now let’s create a UI for the currency converter
To Create UI we will create a class CurrencyConverterUI
- Let’s create the main function.
First, we will create the Converter. Second, Create the UI for Converter
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.
Import Libraries
import requests
from tkinter import *
import tkinter as tk
from tkinter import ttk
Constructor of Class
class RealTimeCurrencyConverter():
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.
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
This method takes the 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 = RealTimeCurrencyConverter(url)
print(converter.convert('INR','USD',100))
Output: 1.33
100 Indian rupees = 1.33 US dollars
Class CurrencyConverterUI
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.
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)
NOTE: This Code part of init method.
# Entry box
valid = (self.register(self.restrictNumberOnly), '%d', '%P')
# restricNumberOnly function will restrict thes user to enter invavalid number in Amount field. We will define it later in code
self.amount_field = Entry(self,bd = 3, relief = tk.RIDGE, justify = tk.CENTER,validate='key', validatecommand=valid)
self.converted_amount_field_label = Label(self, text = '', fg = 'black', bg = 'white', relief = tk.RIDGE, justify = tk.CENTER, width = 17, borderwidth = 3)
# dropdown
self.from_currency_variable = StringVar(self)
self.from_currency_variable.set("INR") # default value
self.to_currency_variable = StringVar(self)
self.to_currency_variable.set("USD") # default value
font = ("Courier", 12, "bold")
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
self.from_currency_dropdown.place(x = 30, y= 120)
self.amount_field.place(x = 36, y = 150)
self.to_currency_dropdown.place(x = 340, y= 120)
#self.converted_amount_field.place(x = 346, y = 150)
self.converted_amount_field_label.place(x = 346, y = 150)
NOTE: This code is part of init
Convert Button
# Convert button
self.convert_button = Button(self, text = "Convert", fg = "black", command = self.perform)
self.convert_button.config(font=('Courier', 10, 'bold'))
self.convert_button.place(x = 225, y = 135)
perform() method:
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))
NOTE: this function is a part of App class.
RestrictNumberOnly() method:
def restrictNumberOnly(self, action, string):
regex = re.compile(r"[0-9,]*?(\.)?[0-9,]*$")
result = regex.match(string)
return (string == "" or (string.count('.') <= 1 and result is not None))
Main Function
if __name__ == '__main__':
url = 'https://api.exchangerate-api.com/v4/latest/USD'
converter = RealTimeCurrencyConverter(url)
App(converter)
mainloop()
Download the Source Code below!
Summary
This Currency Converter with Source Code is free to download.
This project is good for students who want to learn Python programming because this project contains a Graphical User Interface (GUI) and a user user-friendly.
It is easy to understand and manipulate this project and use it for education purposes only.
We worked on a Python project to create a Currency Converter in this article. I hope you learned something new and had fun working on this fun Python project.
Share the story with your friends and colleagues on social media.
Inquiries
If you have any questions or suggestions about Currency Converter In Python with Source Code, please feel free to leave a comment below.