Building a Currency Converter with Python: A Step-by-Step Guide
Introduction:
- The Importance of Currency Conversion:
- Currency conversion is a common requirement in global financial transactions, travel, and international commerce.
- Building a currency converter using Python can simplify and automate the process.
Here is my currency converter python code:
import tkinter as tk
from tkinter import ttk
import requests, json
from bs4 import BeautifulSoup as bs
from tkinter import *
class CurrencyConverter():
def __init__(self,url):
self.data= requests.get(url).json()
self.currencies = self.data['rates']
m = tk.Tk()
m.title('Currency Converter')
m.geometry("300x150")
m.eval('tk::PlaceWindow . center')
Label(m, text='Source Currency :').grid(row=0)
Label(m, text='Target Currency :').grid(row=1)
Label(m, text='Amount :').grid(row=2)
Label(m, text='Result :').grid(row=8)
label=Label(m, text =" ").grid(row=8, column=1)
source_currency_entry = Entry(m)
target_currency_entry = Entry(m)
amount_entry = Entry(m)
source_currency_entry.grid(row=0, column=1)
target_currency_entry.grid(row=1, column=1)
amount_entry.grid(row=2, column=1)
def clear():
source_currency_entry.delete(0, END)
target_currency_entry.delete(0, END)
amount_entry.delete(0, END)
def convert():
source_currency = source_currency_entry.get()
target_currency = target_currency_entry.get()
amount = float(amount_entry.get())
response = requests.get(f"https://www.x-rates.com/calculator/?from={source_currency}&to={target_currency}&amount=1")
soup = bs(response.text, "html.parser")
text1 = soup.find(class_="ccOutputTrail").previous_sibling
text2 = soup.find(class_="ccOutputTrail").get_text(strip=True)
rate = "{}{}".format(text1,text2)
result=amount*float(rate)
#return result
res = Label(m, text =result,font='Helvetica 10 bold')
res.grid(row=8, column=1)
button = tk.Button(m, text='Convert', command=convert)
button.grid(row=3, column=1)
button2 = tk.Button(m, text='Clear', command=clear)
button2.grid(row=4, column=1)
m.mainloop()
Now let’s run our code and make an example:
Let’s convert 10000 dollar to Argentine pesos:
Section 1: Setting Up the Project
1.1 Environment Setup:
- Installing Python and setting up a development environment.
- Utilizing a virtual environment to keep project dependencies isolated.
1.2 Library Installation:
- Introduction to the required libraries:
tkinter
: A standard GUI library for creating the user interface.requests
: A library for making HTTP requests to fetch currency conversion rates.json
: A built-in library for working with JSON data.bs4
(BeautifulSoup): A library for parsing HTML and extracting data from websites.
Section 2: Implementing the CurrencyConverter Class
2.1 Fetching Currency Rates:
- Understanding the API used to fetch live currency rates.
- Utilizing the
requests
library to make a GET request and obtain the rates in JSON format. - Extracting the rates from the API response using the
json
library.
Section 3: Building the User Interface
3.1 Creating the Main Window:
- Importing the necessary modules from
tkinter
. - Setting up the main window with a title and dimensions.
- Centering the window on the screen.
3.2 Adding Labels and Entry Fields:
- Creating labels and entry fields for source currency, target currency, and amount.
- Utilizing the
grid()
method to organize the layout within the window.
3.3 Implementing the Conversion Function:
- Defining the
convert()
function to perform the currency conversion. - Retrieving user input from the entry fields.
- Using the
requests
library to fetch the current conversion rate from a web page. - Parsing the HTML response using
BeautifulSoup
to extract the conversion rate. - Performing the currency conversion calculation.
3.4 Displaying the Result:
- Adding a label to display the converted amount.
- Updating the label with the calculated result.
Section 4: Interacting with the Web
4.1 Making HTTP Requests:
- Discussing the
requests
library in more detail. - Exploring different types of requests and HTTP methods.
4.2 Parsing HTML with BeautifulSoup:
- Understanding the structure of HTML documents.
- Utilizing
BeautifulSoup
to extract specific elements and data from HTML.
Conclusion:
Recap of the process:
- Setting up the project and installing the necessary libraries.
- Implementing the currency converter logic using Python.
- Building the user interface with
tkinter
. - Interacting with the web to fetch live conversion rates.
The benefits of building a currency converter with Python:
- Automation and convenience in currency conversion tasks.
- Potential for customization and integration with other financial applications.
See you in the next articles :)