Currency Converter – Python Source Code Example

Currency Converter – Python Source Code Example

As an Amazon Associate I earn from qualifying purchases.

Currency Converter - Python Source Code Example

A currency converter can use different programming languages. In this article, we will be using the python programming language. For most developers, python is their number one choice when it comes to APIs. The reason is that python can develop many Applications without difficulties. If you are looking forward to having a solid and bright career in Python programming, it is important to have several python projects to work on. Today’s project will shape your destiny in your currency conversion errands using Python.

Currency API – Free Trial

Why is Python Programming Language So Important?

The reason why python is important and a preference of many is that it is easy to use, understand, has been in the market for over 30 years with good python community support. Several sponsors such as Facebook, Google, web services, and Amazon, also back it. Many resources out there offer support as you navigate through your project. Another aspect is the issue of speed, reliability, versatility, flexibility, and efficiency.

Remember, it is next to impossible to execute wise business decisions without a proper understanding of the market trends or having the correct data. Most importantly, understanding different currency values is very vital. There is no way you will get to know this without having a currency converter. However, a currency converter is as good as the Programming Language it uses. In this project, we will be developing a Python Currency Converter.

Let us get down to our project. In this project, you need to have some basic knowledge of Tkinter classic widgets, and the requested modules. We will also need an exchange rate API. We will install Tkinter and Requests libraries by encrypting the code below.

  • pip install tkinter
  • pip install requests

After the code, we will create an account for the project. We do this at the exchange rate API. Then, we have to download the python currency converter string. We will follow the steps below.

  • Importation of the required modules
  • Creation of GUI Window and its heading
  • Inserting the required API data together with the codes
  • Creation of Convert from and to channels
  • Addition of the submit tab and creation of conversion function

Importation of Modules


import requests
from tkinter import *
from tkinter import ttk

Creation of GUI window and its heading


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

To set GUI window

We give the currency converter Window setting its dimensions. After which we get the background coloration otherwise called background attribute. There is the resizable Method, which is responsible for changing the window size as per the user’s requirements. It reflects a (True true), (false false), (height width), and (0 0). To display the static message on the GUI window by any size or color. There comes the place method, the Tkinter-oriented geometry manager that a user sets their starting point.

It takes the options below,

Anchor, for changing the position of 0.0 to bring together the other side or corner of the GUI window.

The horizontal and vertical offsets,- x,y

The Width and Height pixels

The relx and rely-Height & Width the number between 0.0 & 1.0, it is expressed as a fraction of width and height of the parent GUI.

Inserting the required API data and 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]}')

In the exchange rate APIs site, we get our API Key

The Request. get, sends the GET Request website and returns the resultant code

Response.json, replacement of response with the available variable –code 200. It accommodates changing the variable from its former status to JSON formula.

Creation of Convert from APP


# '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")))

Creation of Convert to


# '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")))

Here we come across the Combobox a combination of the entry widgets and options menu. It comes in 3 forms, read-only, normal, and disabled. The read-only set up prevents the user from inserting custom values. The Normal mechanism permits the user to insert entries or values. Finally, Disabled mode disengages the Combobox for some time.

The entry Widget facilitates a space for inserting text or message. With String Var, the user can alter the value of the entry by setting up a text variable feature. Afterward, the user uses the .set format (StringVar) that would change the value of the StringVar object.

Addition of the Submit and conversion buttons

Submit button string

# 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']))

As you can see in the above scenario, we have been able to add both submit and conversion functions. So far, we have been able to set up a code that entails the converted currency, the base currency, and the outcome. You can also see exchange rate conversions very clearly in the above code. Data passed via JSON, giving the converted rate and the conversion result. The above code leads us to the Python Currency Converter.

Python Currency Converter

Python Currency Converter

 

 

 

 

 

 

 

As you can see above, we have been able to construct a python currency converter using a few simple code lines. It is yours for use. We desire that you continue practicing this on your own as you continue to learn slowly.
Free Trial

Amazon and the Amazon logo are trademarks of Amazon.com, Inc, or its affiliates.

Share