使用 Python Tkinter 的即時貨幣轉換器


在當今全球化的世界中,貨幣轉換在各種金融交易和國際貿易中扮演著至關重要的角色。無論您是計劃海外旅行、管理海外投資還是經營跨國公司,訪問即時貨幣兌換都是必不可少的。在本文中,我們將探討如何使用流行的 GUI 工具包 Python Tkinter 構建即時貨幣轉換器。透過利用 Python 和 Tkinter 的強大功能,我們可以建立一個直觀且使用者友好的應用程式,該應用程式不僅可以執行精確的貨幣轉換,還可以為使用者提供直觀的介面來輸入所需的金額並選擇基礎和目標貨幣。在本文中,我們將深入探討基本演算法和逐步實現,並展示滿足不同需求的不同方法。

使用 Python Tkinter 的貨幣轉換器

使用 Python Tkinter 進行即時貨幣轉換包括建立一個圖形使用者介面 (GUI) 應用程式,允許使用者即時轉換貨幣。該應用程式遵循一個逐步的過程,從使用者輸入要轉換的金額和選擇基礎和目標貨幣開始。為了獲取即時匯率,該應用程式與貨幣匯率 API 互動。這可以使用“requests”庫來完成,該庫允許對 API 端點發出 HTTP 請求。API 返回一個包含各種貨幣最新匯率的 JSON 響應。該應用程式從 API 響應中提取所選基礎貨幣和目標貨幣之間的轉換率。

方法

  • 方法 1 - 使用 Web API 進行貨幣轉換

  • 方法 2 - 使用預載入的貨幣轉換表

  • 方法 3 - 使用 Python 庫進行貨幣轉換

方法 1:使用 Web API 進行貨幣轉換

主要方法包括使用 Web API 獲取即時貨幣匯率並執行轉換。可以使用知名的 API,例如 Open Exchange Rates 或 Exchange Rate-API 來獲取最新匯率。以下是執行此方法的步驟:

演算法

  • 步驟 1 - 引入所需的包。

  • 步驟 2 - 建立函式 convert_currency()。

  • 步驟 3 - 呼叫 get() 方法從使用者那裡獲取金額、base_currency 和 target_currency。

  • 步驟 4 - 獲取即時匯率。

  • 步驟 5 - 設定視窗上的標籤,最後執行應用程式。

示例

import tkinter as tk
import requests

def convert_currency():
    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=str(converted_amount))

window = tk.Tk()
window.title("Currency Converter")

label1 = tk.Label(window, text="Amount:")
label1.pack()

entry = tk.Entry(window)
entry.pack()

label2 = tk.Label(window, text="Base Currency:")
label2.pack()

base_currency_var = tk.StringVar(window)
base_currency_var.set("USD")  # Default base currency

base_currency_dropdown = tk.OptionMenu(window, base_currency_var, "USD", "EUR", "GBP", "INR")
base_currency_dropdown.pack()

label3 = tk.Label(window, text="Target Currency:")
label3.pack()

target_currency_var = tk.StringVar(window)
target_currency_var.set("EUR")  # Default target currency

target_currency_dropdown = tk.OptionMenu(window, target_currency_var, "USD", "EUR", "GBP", "INR")
target_currency_dropdown.pack()

label4 = tk.Label(window, text="Converted Amount:")
label4.pack()

result_label = tk.Label(window, text="")
result_label.pack()

button = tk.Button(window, text="Convert", command=convert_currency)
button.pack()

window.mainloop()

輸出

執行應用程式後,將出現一個 GUI 視窗,其中包含金額的輸入欄位、轉換金額的標籤和“轉換”按鈕。使用者可以在輸入欄位中輸入金額,然後單擊“轉換”按鈕以在結果標籤中檢視轉換後的金額。

方法 2:使用預載入的貨幣轉換表

在某些情況下,訪問 Web API 可能不可行。在這種情況下,我們可以使用預載入的貨幣轉換表。以下是此方法的步驟:

執行程式程式碼所需的多步驟如下:

  • 步驟 1 - 匯入 tkinter 模組。建立一個貨幣轉換表。

  • 步驟 2 - 匯入所需的庫並讀取轉換表。

  • 步驟 3 - 定義名為 convert_currency() 的函式,並編寫必要的程式碼和條件以從使用者那裡獲取貨幣。

  • 步驟 4 - 執行應用程式。

示例

import tkinter as tk

conversion_table = {
    "USD": {
        "EUR": 0.85,
        "GBP": 0.72,
        "JPY": 109.71,
        "CAD": 1.22,
        "AUD": 1.32
    },
    "EUR": {
        "USD": 1.18,
        "GBP": 0.85,
        "JPY": 129.67,
        "CAD": 1.47,
        "AUD": 1.59
    },
    "GBP": {
        "USD": 1.39,
        "EUR": 1.18,
        "JPY": 151.37,
        "CAD": 1.70,
        "AUD": 1.84
    },
    "JPY": {
        "USD": 0.0091,
        "EUR": 0.0077,
        "GBP": 0.0066,
        "CAD": 0.011,
        "AUD": 0.012
    },
    "CAD": {
        "USD": 0.82,
        "EUR": 0.68,
        "GBP": 0.59,
        "JPY": 87.47,
        "AUD": 1.08
    },
    "AUD": {
        "USD": 0.76,
        "EUR": 0.63,
        "GBP": 0.54,
        "JPY": 81.75,
        "CAD": 0.93
    }
}

def convert_currency():
    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=str(converted_amount))

window = tk.Tk()
window.title("Currency Converter")

label1 = tk.Label(window, text="Amount:")
label1.pack()

entry = tk.Entry(window)
entry.pack()

label2 = tk.Label(window, text="Base Currency:")
label2.pack()

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.pack()

label3 = tk.Label(window, text="Target Currency:")
label3.pack()

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.pack()

label4 = tk.Label(window, text="Converted Amount:")
label4.pack()

result_label = tk.Label(window, text="")
result_label.pack()

button = tk.Button(window, text="Convert", command=convert_currency)
button.pack()

window.mainloop()

輸出

結論

總之,我們探討了如何使用 Python Tkinter 構建即時貨幣轉換器。我們研究了三種不同的方法,包括使用 Web API、預載入的貨幣轉換表和用於貨幣轉換的 Python 庫。每種方法都有其優點,可以選擇根據具體需求選擇。透過遵循逐步執行並理解所涉及的演算法,使用者可以輕鬆建立貨幣轉換器應用程式並執行即時貨幣轉換。

更新於:2023年9月4日

997 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告