Currency Converter – Python Project with Source Code
Want a Python career? Start building! Debugshala brings hands-on projects to make you job-ready.
In this tutorial, we’ll build a fun and practical Python project – a Currency Converter. We'll use the tkinter library to create a simple user interface.
Currency Converter in Python
Prerequisites
To work on this Python currency converter project, you should know basic Python and how to work with external libraries.
tkinter – for building the User Interface (UI)
requests – for accessing real-time exchange rate data from an API
To install the libraries, use the following commands in your terminal:
bash
pip install tkinter pip install requests
Steps to Build the Python Currency Converter Project
1. Real-time Exchange Rates
We'll use real-time exchange data from a public API. This API returns JSON data that includes:
Base – Usually in USD (US Dollar)
Date and Time – Shows when the data was last updated
Rates – Shows currency rates compared to the base currency
2. Import Required Libraries
python
import requests from tkinter import * import tkinter as tk from tkinter import ttk
3. Create the CurrencyConverter Class
3.1 Constructor
python
class RealTimeCurrencyConverter(): def __init__(self, url): self.data = requests.get(url).json() self.currencies = self.data['rates']
This loads JSON data from the API and stores currency rates.
3.2 Convert Method
python
def convert(self, from_currency, to_currency, amount): initial_amount = amount if from_currency != 'USD': amount = amount / self.currencies[from_currency] amount = round(amount * self.currencies[to_currency], 4) return amount
This method converts one currency to another using USD as a base.
Example:
python
url = 'https://api.exchangerate-api.com/v4/latest/USD' converter = RealTimeCurrencyConverter(url) print(converter.convert('INR','USD',100))
Output:
1.33
4. Build the User Interface (UI)
We’ll use tkinter to create a graphical interface.
4.1 Initialize the UI
python
def __init__(self, converter): tk.Tk.__init__(self) self.title = 'Currency Converter' self.currency_converter = converter
This initializes the UI window.
4.2 Add Labels and Info
python
self.geometry("500x200") self.intro_label = Label(self, text='Welcome to Real Time Currency Converter', fg='blue', relief=tk.RAISED, borderwidth=3) self.intro_label.config(font=('Courier', 15, 'bold')) self.date_label = Label(self, text=f"1 INR = {self.currency_converter.convert('INR','USD',1)} USD \nDate: {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)
4.3 Add Entry and Dropdowns
python
valid = (self.register(self.restrictNumberOnly), '%d', '%P') 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) self.from_currency_variable = StringVar(self) self.from_currency_variable.set("INR") self.to_currency_variable = StringVar(self) self.to_currency_variable.set("USD") 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.currencies.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.currencies.keys()), font=font, state='readonly', width=12, justify=tk.CENTER) 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_label.place(x=346, y=150)
4.4 Add Convert Button
python
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)
5. Create the perform() Method
python
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))
6. Restrict Non-numeric Input
python
def restrictNumberOnly(self, action, string): regex = re.compile(r"[0-9,]*?(\.)?[0-9,]*$") result = regex.match(string) return (string == "" or (string.count('.') <= 1 and result is not None))
7. Create the Main Function
python
if __name__ == '__main__': url = 'https://api.exchangerate-api.com/v4/latest/USD' converter = RealTimeCurrencyConverter(url) App(converter) mainloop()
Summary
In this project, we learned how to build a real-time currency converter using Python. We used tkinter for the interface and an API for fetching current exchange rates. This kind of project helps in improving both your Python programming and UI development skills.
Write A Comment
No Comments