Real-time Currency Converter with Python

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.

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

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.

  • Tkinter: To create the GUI components of this app, ranging from the widgets to the window.
  • Requests: To get the HTTP requests to communicate with the servers of the ExchangeRate API directly.

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:

  • title() method – This gives a title to the python currency converter window.
  • geometry() method – This sets the initial dimensions to the window.
  • background attribute – This attribute helps in giving a background color to the window.
  • resizable() method – This method allows the currency converter window to change its size according to the user’s need or not. It takes the positional arguments in the form (height, width) and the defaults are (True, True) but you can also change to (False, False) or (0, 0) to set a fixed size to the window.
  • Label widget – This is used to display static text on a GUI window of any font, or any background color.
  • place() method – This is the Tkinter geometry manager method that allows the user to set the starting position of a widget as per the coordinates on a cartesian plane, with (0, 0) being the northwest (default) corner of the GUI window. It takes the following options:
    • anchor – This is useful to change the position of (0,0) coordinate to any other corner/side of the window.
    • x, y – These are the horizontal and vertical offsets in pixels.
    • height, width – These are the height and width in pixels.
    • relx, rely – Height and width as a floating-point number between 0.0 and 1.0, as a fraction of the height and width of the parent window.
    • relheight, relwidth – (same as relx, rely)

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:

  • In the API variable, you will put your own API key that you will get after you register at ExchangeRate API’s website.
  • requests.get() – This function takes 1 argument that must be a URL. This function sends a GET request to the web page and returns the status code of that page.
  • response.json() – <Replace response with any variable that has a status code 200> This function is used to convert the data from the variable, in this case ‘response’, into the JSON format, in the form of a data structure, such that it can be parsed like a dictionary. The variable should be in the requests.get(url) form and the status code of it should be 200.

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.

  • The 3 modes of ComboBox are in state attribute are:
    • ‘readonly’ – prevents the users from entering custom values
    • ‘normal’ – allows the user to enter a new entry/value to the ComboBox list
    • ‘disabled’ – disables the ComboBox for a while
  • The values attribute stores all the values that are to be displayed in the dropdown menu of the Combobox.
  • The .current() method of Combobox sets the default value that will be displayed.

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:

  • Entry widget – The Entry widget is a widget in Tkinter that acts as space where text can be entered.
  • With the help of the StringVar class, you can change the value of your Entry widget by setting the textvariable attribute of Entry as a StringVar object and then using the .set() method of the StringVar to change its(StringVar object’s) value.

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:

  • master – window where the widget will be displayed
  • options: (These are the ones discussed above)
    • text – text to be displayed
    • font – font family to be used to display the text. It goes in the form, (‘family’, size, ‘bold'(optional), ‘italic'(optional))
    • bg/background – background to be set to the text.
    • command – command to be performed when the button is pressed. It is generally a function with no parameters, but to set the command to a function with arguments, you have to use the lambda keyword.

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.