Hướng dẫn tạo ứng dụng chuyển đổi tiền tệ với Python từ A-Z

Đăng ngày: 20/06/2026 • Tác giả: Quản trị viên
Hướng dẫn tạo ứng dụng chuyển đổi tiền tệ với Python từ A-Z

Hướng dẫn tạo ứng dụng chuyển đổi tiền tệ với Python từ A-Z

Giới thiệu

Trong bài viết này, chúng ta sẽ xây dựng một ứng dụng chuyển đổi tiền tệ bằng Python sử dụng Tkinter để tạo giao diện và API của ExchangeRate-API để lấy tỷ giá tiền tệ.

1. Cấu trúc thư mục dự án

Chúng ta sẽ tổ chức dự án thành các tệp sau:

CurrencyConverter/
│── main.py          # Tệp chính để chạy ứng dụng
│── gui.py           # Xây dựng giao diện bằng Tkinter
│── currency.py      # Xử lý lấy dữ liệu tỷ giá

2. Lấy API Key từ ExchangeRate-API

3. Xử lý dữ liệu tỷ giá tiền tệ

Tạo tệp currency.py để gọi API và lấy tỷ giá tiền tệ.

# currency.py
import requests

API_KEY = "YOUR_API_KEY"  # Thay YOUR_API_KEY bằng API thực tế của bạn
BASE_URL = "https://v6.exchangerate-api.com/v6/{}/latest/".format(API_KEY)

def get_exchange_rate(base_currency, target_currency):
    response = requests.get(BASE_URL + base_currency)
    if response.status_code == 200:
        data = response.json()
        return data["conversion_rates"].get(target_currency, None)
    else:
        return None

4. Thiết kế giao diện với Tkinter

Tạo tệp gui.py để xây dựng giao diện.

# gui.py
import tkinter as tk
from tkinter import messagebox
import currency

def convert_currency():
    base_currency = base_currency_entry.get().upper()
    target_currency = target_currency_entry.get().upper()
    amount = amount_entry.get()
    
    if not base_currency or not target_currency or not amount:
        messagebox.showwarning("Lỗi", "Vui lòng nhập đầy đủ thông tin!")
        return
    
    try:
        amount = float(amount)
        rate = currency.get_exchange_rate(base_currency, target_currency)
        if rate:
            converted_amount = amount * rate
            result_var.set(f"{amount} {base_currency} = {converted_amount:.2f} {target_currency}")
        else:
            messagebox.showerror("Lỗi", "Không tìm thấy tỷ giá hối đoái!")
    except ValueError:
        messagebox.showerror("Lỗi", "Số tiền không hợp lệ!")

def create_gui(root):
    global base_currency_entry, target_currency_entry, amount_entry, result_var
    
    tk.Label(root, text="Chuyển đổi tiền tệ", font=("Arial", 16)).pack()
    
    tk.Label(root, text="Nhập đơn vị tiền gốc (VD: USD):").pack()
    base_currency_entry = tk.Entry(root)
    base_currency_entry.pack()
    
    tk.Label(root, text="Nhập đơn vị tiền đích (VD: VND):").pack()
    target_currency_entry = tk.Entry(root)
    target_currency_entry.pack()
    
    tk.Label(root, text="Nhập số tiền:").pack()
    amount_entry = tk.Entry(root)
    amount_entry.pack()
    
    tk.Button(root, text="Chuyển đổi", command=convert_currency).pack(pady=5)
    
    result_var = tk.StringVar()
    result_label = tk.Label(root, textvariable=result_var, font=("Arial", 14))
    result_label.pack()

5. Chạy ứng dụng

Tạo tệp main.py để khởi chạy ứng dụng.

# main.py
import tkinter as tk
import gui

root = tk.Tk()
root.title("Chuyển đổi tiền tệ")
gui.create_gui(root)
root.mainloop()

Kết luận

Vậy là chúng ta đã hoàn thành một ứng dụng chuyển đổi tiền tệ với Python. Bạn có thể mở rộng thêm tính năng như lưu lịch sử chuyển đổi, biểu đồ tỷ giá theo thời gian, hoặc chuyển đổi nhiều loại tiền cùng lúc để làm ứng dụng hữu ích hơn!

Bài viết liên quan