Site icon TechVidvan

Real-time Currency Converter with Python

python project currency converter

In this python project, we will create a gui based currency converter using tkinter and requests libraries of python.

It is a beginner-to-intermediate project where you will learn to use web APIs, HTTP requests, and a GUI module together and learn interesting things about basic python concepts.

Currency Converter:

Currency converter is a software that converts the real value of one currency into another country’s currency so that you can convert between the two currencies. Currency converter is useful when we make transactions in multiple currencies or when we deal in more than one currencies.

About Currency Converter Project:

This project will help you to create your own personal currency converter using python.

You will only need basic knowledge about the python, requests module, and some intermediate knowledge about the Tkinter library as well. You will also need knowledge about the Tkinter themed widgets, one of which we will be using here).

Project Prerequisites:

To build this python currency converter project, you need to have basic knowledge of the requests module, Tkinter classic widgets, and knowledge about Tkinter themed widgets; and you will require the ExchangeRate API.

To install the tkinter and requests library, type the following code in your terminal:

pip install tkinter
pip install requests

You will also need to create an account at the ExchangeRate API for this project. Don’t worry, it’s the most reliable and open-source (free) library for currency conversion on the internet [and they don’t send you incessant emails as well]

Download Currency Converter Project Code

Before proceeding ahead, please download source code of python currency converter project: Currency Converter Project Code

Currency Converter Project File Structure:

Below are the steps we will follow to create currency converter project:

  1. Importing the necessary modules
  2. Creating a master GUI window and creating a heading for it
  3. Entering the basic information of the API and creating a list of the codes
  4. Creating the ‘Convert from’ portion of our app
  5. Creating the ‘Convert to’ portion of our app
  6. Adding the ‘Submit’ button and creating the conversion function.

Let’s take a closer look at all of these steps:

1. Importing the necessary modules:

import requests
from tkinter import *
from tkinter import ttk

2. Creating the master GUI window and creating a heading for it:

# Creating a GUI window for the project
root = Tk()
root.title("TechVidvan Currency Converter")

root.geometry('500x250')
root.resizable(0, 0)
root.configure(bg='RoyalBlue')


# Creating a Heading for the window
Label(root, text='TechVidvan Currency Converter', font=('Comic Sans MS', 18), bg='RoyalBlue').place(x=70)

# Finalizing the GUI
root.update()
root.mainloop()

Explanation:

While setting a master GUI window, we need to use below methods:

3. Entering the basic information of the API and creating a list of the codes:

# Basic information about API
api = 'YOUR_API_KEY_HERE'


# Creating a StringVar object of the list of currencies
li_currencies = list()   # Variable name 'li_currencies' is a contraction of 'list_of_currencies'

codes = f'https://v6.exchangerate-api.com/v6/{api}/codes'
codes_res = requests.get(codes)

for pair in codes_res.json()['supported_codes']:
    li_currencies.append(f'{pair[0]} - {pair[1]}')

Explanation:

4. Creating the ‘Convert from’ portion of our app:

Let’s create convert from part of python currency converter:

# 'Convert from' portion of the window
Label(root, text='Convert from:', font=('Georgia', 13, 'italic'), bg='RoyalBlue').place(x=60, y=60)

amnt_from = Entry(root, width=25)
amnt_from.place(x=45, y=100)

FROM__currency_names = ttk.Combobox(root, state='readonly', values=li_currencies, width=30)
FROM__currency_names.place(x=20, y=140)
FROM__currency_names.current((li_currencies.index("INR - Indian Rupee")))

Explanation:

Combobox – It is a Tkinter themed widget that acts as a combination of the classic OptionMenu and the Entry widgets.

5. Creating the ‘Convert to’ portion of our app:

# 'Convert to' portion of the window
Label(root, text='Convert to:', font=('Georgia', 13, 'italic'), bg='RoyalBlue').place(x=330, y=60)

converted_currency = StringVar(root)
amnt_to = Entry(root, width=25, textvariable=converted_currency)
amnt_to.place(x=300, y=100)

TO__currency_names = ttk.Combobox(root, state='readonly', values=li_currencies, width=30)
TO__currency_names.place(x=275, y=140)
TO__currency_names.current((li_currencies.index("INR - Indian Rupee")))

Explanation:

6. Adding the ‘Submit’ button and creating the conversion function:

Submit button:

# Submit Button
submit_btn = Button(root, text='Submit', bg='SpringGreen', command=lambda: convert_currency(api, converted_currency, FROM__currency_names.get(), TO__currency_names.get(), amnt_from.get()))

submit_btn.place(x=225, y=190)

Conversion function:

# Conversion Function
def convert_currency(your_api_code, converted_rate, from_, to, amount):
    data = requests.get(f'https://v6.exchangerate-api.com/v6/{your_api_code}/pair/{from_[:3]}/{to[:3]}/{amount}')

    res = data.json()

    converted_rate.set(str(res['conversion_result']))

Explanation:

Button widget – Used to add a button to the currency converter window. It takes the following parameters:

In the last line of the Conversion function, we had to convert the type of res[‘conversion_result’] because the StringVar object only accepts string type variables to be set as values, but res-conversion_result value was actually a floating point object.

Python Currency Converter Output

Summary

We have finally created our own python currency converter using only a few lines of codes! We also learned about another usage of StringVar (to allot text to Entry widgets), and about Combobox, one of the newer widgets part of the Themed Widgets group.

Exit mobile version