Visitar URL original
Real time currency converter using Python Tkinter

Real time currency converter using Python Tkinter

In today's globalized world, currency conversion plays an essential role in various financial transactions and international exchanges. Whether you're planning a trip overseas, managing foreign investments, or running multinational business, having access to real-time currency conversion is crucial. In this article, we'll explore how to build a real-time currency converter using Python Tkinter, a popular GUI toolkit.

By leveraging the power of Python and Tkinter, we can create an intuitive and user-friendly application that performs accurate currency conversions with an interactive interface for users to input amounts and select currencies.

Currency Converter using Python Tkinter

Real-time currency conversion using Python Tkinter involves creating a graphical user interface (GUI) application that allows users to convert currencies on-the-fly. The application follows a step-by-step process where users enter an amount and select base and target currencies. To fetch real-time exchange rates, the application integrates with a currency exchange rate API using the requests library.

Approaches

  • Approach 1 ? Using Web API for real-time conversion rates

  • Approach 2 ? Using a preloaded currency conversion table

Approach 1: Using Web API for Currency Conversion

This approach uses a web API to fetch real-time currency exchange rates. We'll use the Exchange Rate API to get the latest exchange rates ?

Algorithm

  • Step 1 ? Import required packages

  • Step 2 ? Create a function convert_currency()

  • Step 3 ? Get user input for amount, base currency, and target currency

  • Step 4 ? Fetch real-time exchange rates from API

  • Step 5 ? Set up GUI components and run the application

Example

import tkinter as tk
import requests

def convert_currency():
    try:
        amount = float(entry.get())
        base_currency = base_currency_var.get()
        target_currency = target_currency_var.get()

        response = requests.get(f"https://api.exchangerate-api.com/v4/latest/{base_currency}")
        data = response.json()

        conversion_rate = data['rates'][target_currency]
        converted_amount = amount * conversion_rate

        result_label.config(text=f"{converted_amount:.2f} {target_currency}")
    except ValueError:
        result_label.config(text="Please enter a valid number")
    except Exception as e:
        result_label.config(text="Error fetching exchange rates")

window = tk.Tk()
window.title("Real-Time Currency Converter")
window.geometry("300x400")

# Amount input
label1 = tk.Label(window, text="Amount:", font=("Arial", 12))
label1.pack(pady=5)

entry = tk.Entry(window, font=("Arial", 12), width=20)
entry.pack(pady=5)

# Base currency selection
label2 = tk.Label(window, text="From Currency:", font=("Arial", 12))
label2.pack(pady=5)

base_currency_var = tk.StringVar(window)
base_currency_var.set("USD")

base_currency_dropdown = tk.OptionMenu(window, base_currency_var, "USD", "EUR", "GBP", "JPY", "INR", "CAD", "AUD")
base_currency_dropdown.config(font=("Arial", 10))
base_currency_dropdown.pack(pady=5)

# Target currency selection
label3 = tk.Label(window, text="To Currency:", font=("Arial", 12))
label3.pack(pady=5)

target_currency_var = tk.StringVar(window)
target_currency_var.set("EUR")

target_currency_dropdown = tk.OptionMenu(window, target_currency_var, "USD", "EUR", "GBP", "JPY", "INR", "CAD", "AUD")
target_currency_dropdown.config(font=("Arial", 10))
target_currency_dropdown.pack(pady=5)

# Convert button
button = tk.Button(window, text="Convert", command=convert_currency, font=("Arial", 12), bg="lightblue")
button.pack(pady=10)

# Result display
label4 = tk.Label(window, text="Converted Amount:", font=("Arial", 12))
label4.pack(pady=5)

result_label = tk.Label(window, text="", font=("Arial", 14, "bold"), fg="green")
result_label.pack(pady=10)

window.mainloop()

Output

After running the application, a GUI window appears with input fields for the amount, dropdown menus for currency selection, and a "Convert" button. Users can enter an amount and click "Convert" to see the real-time converted amount.

Approach 2: Using Preloaded Currency Conversion Table

In scenarios where internet access is limited, we can use a preloaded currency conversion table with static exchange rates ?

Example

import tkinter as tk

conversion_table = {
    "USD": {
        "EUR": 0.85,
        "GBP": 0.72,
        "JPY": 109.71,
        "CAD": 1.22,
        "AUD": 1.32,
        "INR": 74.50
    },
    "EUR": {
        "USD": 1.18,
        "GBP": 0.85,
        "JPY": 129.67,
        "CAD": 1.47,
        "AUD": 1.59,
        "INR": 87.85
    },
    "GBP": {
        "USD": 1.39,
        "EUR": 1.18,
        "JPY": 151.37,
        "CAD": 1.70,
        "AUD": 1.84,
        "INR": 103.50
    }
}

def convert_currency():
    try:
        amount = float(entry.get())
        base_currency = base_currency_var.get()
        target_currency = target_currency_var.get()

        if base_currency == target_currency:
            converted_amount = amount
        else:
            conversion_rate = conversion_table[base_currency][target_currency]
            converted_amount = amount * conversion_rate

        result_label.config(text=f"{converted_amount:.2f} {target_currency}")
    except ValueError:
        result_label.config(text="Please enter a valid number")
    except KeyError:
        result_label.config(text="Currency pair not available")

window = tk.Tk()
window.title("Currency Converter (Offline)")
window.geometry("300x400")

# Amount input
label1 = tk.Label(window, text="Amount:", font=("Arial", 12))
label1.pack(pady=5)

entry = tk.Entry(window, font=("Arial", 12), width=20)
entry.pack(pady=5)

# Base currency selection
label2 = tk.Label(window, text="From Currency:", font=("Arial", 12))
label2.pack(pady=5)

base_currency_var = tk.StringVar()
base_currency_var.set("USD")

base_currency_menu = tk.OptionMenu(window, base_currency_var, *conversion_table.keys())
base_currency_menu.config(font=("Arial", 10))
base_currency_menu.pack(pady=5)

# Target currency selection
label3 = tk.Label(window, text="To Currency:", font=("Arial", 12))
label3.pack(pady=5)

target_currency_var = tk.StringVar()
target_currency_var.set("EUR")

target_currency_menu = tk.OptionMenu(window, target_currency_var, *conversion_table.keys())
target_currency_menu.config(font=("Arial", 10))
target_currency_menu.pack(pady=5)

# Convert button
button = tk.Button(window, text="Convert", command=convert_currency, font=("Arial", 12), bg="lightgreen")
button.pack(pady=10)

# Result display
label4 = tk.Label(window, text="Converted Amount:", font=("Arial", 12))
label4.pack(pady=5)

result_label = tk.Label(window, text="", font=("Arial", 14, "bold"), fg="blue")
result_label.pack(pady=10)

window.mainloop()

Comparison

Approach Data Source Accuracy Internet Required Best For
Web API Real-time rates High Yes Live applications
Preloaded Table Static rates Moderate No Offline use

Conclusion

We explored how to build a currency converter using Python Tkinter with two different approaches. The Web API approach provides real-time accuracy, while the preloaded table approach works offline. Choose the approach based on your specific requirements for internet connectivity and data accuracy.

Updated on: 2026-03-27T14:29:06+05:30

2K+ Views

Advertisements