Currency Converter - Python Project With Source Code: Search For
Currency Converter - Python Project With Source Code: Search For
Currency Converter - Python Project With Source Code: Search For
Blog Home
Data Science
Categories
Courses
Search for:
In this tutorial, we are going to build an exciting python project through which you
can convert currencies. For a user interface, we are going to use the tkinter library
Currency Converter in Python
Prerequisites
The currency converter project in python requires you to have basic knowledge of
python programming and the pygame library.
To install the tkinter and requests library, type the following code in your terminal:
pip install tkinter
pip install requests
Here, we can see the data in JSON format, with the following details:
Base – USD: It means we have our base currency USD. which means to convert
any currency we have to first convert it to USD then from USD, we will convert it
in whichever currency we want.
Date and time: It shows the last updated date and time.
Rates: It is the exchange rate of currencies with base currency USD.
import requests
from tkinter import *
import tkinter as tk
from tkinter import ttk
class RealTimeCurrencyConverter():
def __init__(self,url):
self.data= requests.get(url).json()
self.currencies = self.data['rates']
requests.get(url) load the page in our python program and then .json() will convert
the page into the json file. We store it in a data variable.
Example:
url = 'https://api.exchangerate-api.com/v4/latest/USD'
converter = RealTimeCurrencyConverter(url)
print(converter.convert('INR','USD',100))
OUTPUT: 1.33
self.geometry("500x200")
#Label
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 :
{self.currency_converter.data['date']}", relief = tk.GROOVE, borderwidth = 5)
self.intro_label.place(x = 10 , y = 5)
self.date_label.place(x = 170, y= 50)
First, we set up the frame and add some info in it. After the execution of this part
of code, our frame looks like something.
Now let’s create the entry box for the amount and options of currency in the frame.
So That users can enter the amount and choose among currencies.
# 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.curre
ncies.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.currenci
es.keys()), font = font, state = 'readonly', width = 12, justify = tk.CENTER)
# placing
self.from_currency_dropdown.place(x = 30, y= 120)
self.amount_field.place(x = 36, y = 150)
self.to_currency_dropdown.place(x = 340, y= 120)
#self.converted_amount_field.place(x = 346, y = 150)
self.converted_amount_field_label.place(x = 346, y = 150)
After the successful Execution of code till now. We will get below screen:
Now Let’s add the CONVERT button which will call the perform function.
# Convert button
self.convert_button = Button(self, text = "Convert", fg = "black", command =
self.perform)
self.convert_button.config(font=('Courier', 10, 'bold'))
self.convert_button.place(x = 225, y = 135)
perform() method:
The perform method will take the user input and convert the amount into the
desired currency and display it on the converted_amount entry box.
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))
RestrictNumberOnly() method:
Now let’s create a restriction in our entry box. So that user can enter only a
number in Amount Field. We have discussed earlier that this will be done by our
RrestricNumberOnly method.
if __name__ == '__main__':
url = 'https://api.exchangerate-api.com/v4/latest/USD'
converter = RealTimeCurrencyConverter(url)
App(converter)
mainloop()
Summary
In this article, we worked on the Python project to build a Currency Converter.
I hope you learned new things and enjoyed building this interesting Python project.
Share the article on social media with your friends and colleagues.
22 Responses
Comments22
Pingbacks0
1. Mark
I followed all the steps up until the last bit where I got stuck and my code isn’t working. help?
error says RealTimeCurrencyConverter doesn’t exist
Reply
o DataFlair Team
The code in the article has been updated, alternatively you can download the code
from the download section
Reply
2. jonh smith
Reply
3. Riley
Reply
o DataFlair Team
Reply
4. PussySlayer69
Reply
5. basil
Can someone plz send me the whole code that i can use for my python class?
Reply
6. Harsh
In the function restrictedNumbersOnly, the name ‘re’ is not defined, help me fix this issue.
Reply
o DataFlair Team
Reply
o DataFlair Team
Reply
7. Yash Gaikwad
url = ‘https://api.exchangerate-api.com/v4/latest/USD’
converter = CurrencyConverter(url)
print(converter.convert(‘INR’,’USD’,100)) to this code I am getting an error like this, please
help
please help
Reply
o DataFlair Team
Reply
8. Brandy Kicks
I’m making my own and tells me that ‘self’ is not defined what should I define it as?
Reply
o DataFlair Team
Self represents instance of the class. It seems you have some syntactical error in your
code, We would recommend you to check your code with the code provided in the
downloads section.
Reply
9. ashar shaikh
Reply
o DataFlair Team
Reply
Reply
o Abhishek Farande
Reply
o DataFlair Team
Reply
11. Anon
At least learn the basics before copy pasting and running this.
Reply
12. Wally
I’m a beginner. Can you tell me where you got the App class?
Reply
Reply
Leave a Reply
Comment
Name *
Email *
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service
apply.
Home About us Contact us Terms and Conditions Privacy Policy Disclaimer Write For Us
Success Stories
Close Ad