Real-time Currency Converter with Python
Currency Converter is a very useful application. Why not try making one on our own using python? Let’s start discussing what a currency converter is and then try to build a currency converter project using python.
About Currency Converter
A currency converter is an application that helps with the quick and easy conversion of currencies on the basis of exchange rates. In simple language, a currency converter helps in converting an amount from one currency to another currency.
A currency converter is generally used while traveling abroad, by businessmen, and while exporting and importing trades.
Creating Currency Converter Project Using Python
There are many different ways to create a GUI using Python like Pycharm but here we will be using Tkinter Module to create GUI for our Currency Converter because Tkinter is easy to use and beginner friendly.
We will create this project from scratch. We will start off by creating a GUI window and will add labels, fields, and buttons which are necessary. Then we will implement the currency converter by importing CurrencyRates from forex_python.converter.
Let’s start implementing the project, that is creating a currency converter:
Follow the steps given below and it would be very easy for you to implement the python currency converter project. So without wasting any more time, let’s get started.
Project Prerequisites
These are the following libraries that you need to install to create the project:
Tkinter module(pip install tk)
forex_python.converter(pip install forex_python)
It is important to have these libraries installed in order to work ahead. If you do not have these, kindly install them using the commands given in the ().
Steps to Create Currency Converter Project using Python
1. Importing necessary Python libraries:
#importing libraries for python currency converter project
from forex_python.converter import CurrencyRates
import tkinter as tk
from tkinter import *
import tkinter.messagebox
Tkinter library – This is for creating an easy and fast GUI in python.
tkinter.messagebox – This is used to display a message box in python. It displays messages according to the system’s requirements.
forex_python.converter – Module for foreign exchange rates and currency conversions.
These libraries are important for creating our project.
2. Creating a GUI Window and Grid:
window = Tk()# creating empty window
window.title("ProjectGurukul Currency Conversion")# adding title to the window
window.geometry("700x400")
head = tk.Label( font=('Helvetica', 19, 'bold'), text='Currency Converter',
bg='grey', fg='black')
head.grid(row=1, column=0, sticky=W)
window= Tk() – for creating an empty GUI window.
Here we have given a title to the window using window.title().
We have created a grid to place the labels. Grid is an imaginary graph-like structure that divides a window into rows and columns and places them as we specify the row and column numbers.
3. Creating and Initializing Variables:
#declaring variables
from_var = tk.StringVar(window)
to_var = tk.StringVar(window)
#initializing variables
from_var.set("currency")
to_var.set("currency")
Creating global two variables- from_var (from variable) and to_var(to variable).
And variables are initialized at “currency”.
4. Defining a function for RealTime Currency Conversions:
#function for realtime conversion
def RealTimeCurrencyConversion():
from forex_python.converter import CurrencyRates
c = CurrencyRates()
from_currency = "currency"
to_currency = "currency"
if (Amount1.get() == ""):
tkinter.messagebox.showinfo("Error !!")
elif (from_currency == "currency" or to_currency == "currency"):
tkinter.messagebox.showinfo("Error !!")
else:
new_amt = c.convert(from_currency, to_currency, float(Amount1.get()))
new_amount = float("{:.4f}".format(new_amt))
Amount2.insert(0, str(new_amount))
get() – This method returns the input as a string. If the field is empty then an error message displays.
If both the currencies are not selected then also display an error message.
Then we convert the string to float.
The converted amount is displayed as Amount2.
5. List Of Currency:
CurrenyCode_list = ["INR", "USD", "CAD", "CNY", "DKK", "EUR"]
Here we specify a list of currencies we will be using in our project.
6. Creating Labels for GUI:
#creating labels
Label_1 = Label(window, font='Helvetica', text="", padx=2, pady=2, bg="grey", fg="black")
Label_1.grid(row=1, column=0, sticky=W)
amount = tk.Label(window, font='Helvetica', text=" Amount : ", bg="grey", fg="black")
amount.grid(row=2, column=0, sticky=W)
from_curr = tk.Label(window, font='Helvetica', text=" From Currency : ", bg="grey", fg="black")
from_curr.grid(row=3, column=0, sticky=W)
to_cur = tk.Label(window, font='Helvetica', text=" To Currency : ", bg="grey", fg="black")
to_cur.grid(row=4, column=0, sticky=W)
convert_amt = tk.Label(window, font='Helvetica', text=" Converted Amount : ", bg="grey", fg="black")
convert_amt.grid(row=8, column=0, sticky=W)
Label_1 = Label(window, font='Helvetica', text="", padx=2, pady=2, bg="grey", fg="black")
Label_1.grid(row=5, column=0, sticky=W)
Label_2 = Label(window, font='Helvetica', text="", padx=2, pady=2, bg="grey", fg="black")
Label_2.grid(row=7, column=0, sticky=W)
Creating labels and then placing them on the grid.
font() – for specifying the font of the text in the label.
text() – specifies the text that will be displayed in that particular label.
Bg- background color.
Fg- foreground color.
Padx,pady – padding of x and y.
Here we have created a total of 4 labels- “From Currency” , “To Currency”, “Amount” and “Converted Amount”.
7. Fetching Data:
FromCurrency = tk.OptionMenu(window, from_var, *CurrenyCode_list)
ToCurrency = tk.OptionMenu(window, to_var, *CurrenyCode_list)
FromCurrency.grid(row=3, column=1, ipadx=45, sticky=E)
ToCurrency.grid(row=4, column=1, ipadx=45, sticky=E)
Amount1 = tk.Entry(window)
Amount1.grid(row=2, column=1, ipadx=28, sticky=E)
Amount2 = tk.Entry(window)
Amount2.grid(row=8, column=1, ipadx=31, sticky=E)
button= Button(window, font='arial', text=" Convert ", padx=2, pady=2, bg="blue", fg="white",
command=RealTimeCurrencyConversion)
button.grid(row=6, column=0)
Label_1 = Label(window, font='Helvetica', text="", padx=2, pady=2, bg="grey", fg="black")
Label_1.grid(row=9, column=0, sticky=W)
window.configure(background='grey')
window.mainloop()
Entry() – provides a text field to enter the amount.
command=RealTimeCurrencyConversion – gives a command to perform the RealTimeCurrencyConversion() function.
Button() – to make conversion we have created a convert button.
Configure()- to configure the window and set background colour as grey.
Mainloop() – This is the command to run the application.
0 Comments