Currency Converter

As an investor, especially where two or more currencies are involved, you will of course want to get the maximum returns out of your investment. However, this cannot be possible if you do not have enough information or knowledge about the value of the currencies in question. The process of converting one currency to the other is known as Currency Conversion. It is, therefore, important to know the value difference between the currencies you trade-in. One of the basic tools to convert one currency to the other is the Currency Converter. Of course, there are many currency converters out there; however, for this article, we will highlight the Python Source Code Currency Converter.

Therefore, whether you are a trader, investor, or someone interested in currency conversion, if you follow this guide to the end, you will qualify to become a Python developer. We are going to develop a simple and easy-to-use Python currency converter. For purpose of this article, for User Interface we will be using the Tkinter file. To be able to execute the entire process successfully, it is important to have some basic knowledge of Python Programming as well as Pygame Library. To start, these two terms will be crucial, Tkinter as UI (User Interface), and Requests for getting URL. The two are to be typed in the terminal.

Before anything else, it is important to download Python Source Code.

Here are steps to follow:

  • Getting real-time exchange rates
  • Importing the required Libraries
  • The currency converter cluster
  • The currency converter User-Interface

Getting Real-time Exchange rates

For real-time currency conversion rates, the JSON format serves better. We are using USD as the Base Currency. This means before converting any other currency, we must convert it to the US dollar. It is only after this that we will be able to convert it to any other desired currency. It will also show the date and time details that it was last updated. It will also show the applicable rates for currencies that have a USD Currency base.

usd code

Importing the Required Libraries

Here we are using Tkinter and requests to complete this assignment. Therefore, we have to Import the concerned Library, as below:

import requests
from tkinter import *
import tkinter as tk
from tkinter import ttk

The Creation of Currency Converter Cluster or Class

It is time to create the currency converter cluster to get real-time conversion or exchange rate. It will convert the concerned currency as well as returning the amount that has been converted. Below is the Constructor of the Class:

class CurrencyConverter():
def __init__(self,url): requests.get(url).json()
self.currencies =['rates']

Requests. Get (URL), the page is loaded in the Python Program. After that, JSON Format converts the page to JSON File then it is stored in the data variable.

The Conversion Method involves:

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

The from currency, from the currency one, wants to convert

To currency, in the currency one wants to convert

Amount refers to the value or amount intended for conversion, as well as returns from the converted Amount. Check below,


url = ''
converter = CurrencyConverter(url)

The output here is 1.33, therefore, 1.33 USD Equals to 100Indian Rupees.

The currency converter User-Interface

Here we are going to develop the currency converter UI, by creating the Class currency converter (UI) as illustrated below,

def __init__(self, converter):
self.title = 'Currency Converter'
self.currency_converter = converte

The above Code forms the frame of the converter.

Below is the format for creating a converter,

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 : {['date']}", relief = tk.GROOVE, borderwidth = 5) = 10 , y = 5) = 170, y= 50)

After setting up the frame, information is added and starts taking shape as below,

python project

Now follows the Entry box that will entail amount and currency options. It allows users to enter the amount as well as choosing among the various available currencies. Below is the code,

# 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 = 30, y= 120) = 36, y = 150) = 340, y= 120) = 346, y = 150) = 346, y = 150)

It takes us to a new screen as below,

python project
It is now time to create the conversion button that will trigger the performance function, Once the command is clicked, it self-performs.

# Convert button
self.convert_button = Button(self, text = "Convert", fg = "black", command = self.perform)
self.convert_button.config(font=('Courier', 10, 'bold')) = 225, y = 135)

This leads to the Performance Method that accepts user input thereby converting the available amount to the desired currency that is displayed in the Entry box of the converted amount,

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))

Creation of the Restrict number only

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))

After the above process, it is time to develop a Restriction mode. It allows for entering a Number only by the user in the created amount field.

Finally, Let us create Main Function

if __name__ == '__main__':
url = ''
converter = RealTimeCurrencyConverter(url)

The first step is creating the converter, then afterward the Converter UI. After the entire process, we have the complete Python Source Code Currency converter as shown on the below screen.

python project
As promised at the beginning of this article we have been able to develop a Currency converter using Python Source Code. It is our hope and belief that you have been able to follow through and learn something. Creating a Currency Converter via Python source code cannot be any better than we have done it.