Live Exchange Rate in Python | Real-Time Currency Converter
Demo :
Click Video πππ
✅ Features:
✔ Real-time currency conversion
✔ Tkinter GUI interface
✔ Fetches live exchange rates
✔ Supports multiple currencies
π Full Source Code + Explanation:
Code :
π Step 1: Install Required Library : cmd => pip install forex-python
import tkinter as tk
from tkinter import ttk, messagebox
import requests
API_KEY = "your_api_key_here" # ⚡ Get your free API key from exchangeratesapi.io
BASE_URL = "https://api.exchangerate-api.com/v4/latest/"
def convert_currency():
try:
amount_text = entry_amount.get().strip()
if not amount_text or not amount_text.replace(".", "").isdigit():
raise ValueError # Agar empty ya invalid input hai toh error raise karo
amount = float(amount_text)
from_currency = combo_from.get()
to_currency = combo_to.get()
if not from_currency or not to_currency:
messagebox.showerror("Error", "Please select both currencies.")
return
# π API Call
response = requests.get(f"{BASE_URL}{from_currency}")
if response.status_code != 200:
messagebox.showerror("Error", "Currency rates not available. Try again later.")
return
data = response.json()
if to_currency not in data["rates"]:
messagebox.showerror("Error", "Invalid currency selected.")
return
converted_amount = amount * data["rates"][to_currency]
result_label.config(text=f"Converted Amount: {converted_amount:.2f} {to_currency}")
except ValueError:
messagebox.showerror("Error", "Please enter a valid number.")
except requests.exceptions.RequestException:
messagebox.showerror("Error", "Network error. Please check your internet connection.")
# GUI Setup
root = tk.Tk()
root.title("Currency Converter")
root.geometry("400x300")
root.configure(bg="#222")
title_label = tk.Label(root, text="Currency Converter", font=("Arial", 16, "bold"), bg="#222", fg="white")
title_label.pack(pady=10)
entry_amount = tk.Entry(root, font=("Arial", 14), justify="center")
entry_amount.pack(pady=5)
entry_amount.insert(0, "1") # Default value
currencies = ["USD", "INR", "EUR", "GBP", "JPY", "CAD", "AUD", "CNY"]
frame = tk.Frame(root, bg="#222")
frame.pack(pady=10)
combo_from = ttk.Combobox(frame, values=currencies, font=("Arial", 12), state="readonly")
combo_from.set("USD")
combo_from.grid(row=0, column=0, padx=5)
swap_label = tk.Label(frame, text="→", font=("Arial", 14), fg="white", bg="#222")
swap_label.grid(row=0, column=1, padx=5)
combo_to = ttk.Combobox(frame, values=currencies, font=("Arial", 12), state="readonly")
combo_to.set("INR")
combo_to.grid(row=0, column=2, padx=5)
convert_button = tk.Button(root, text="Convert", font=("Arial", 14), bg="#ff5733", fg="white", command=convert_currency)
convert_button.pack(pady=10)
result_label = tk.Label(root, text="Converted Amount: ", font=("Arial", 14), bg="#222", fg="white")
result_label.pack(pady=10)
root.mainloop()
Comments
Post a Comment