使用 Python Tkinter 的实时货币转换器
在当今的全球化世界中,货币兑换在各种货币兑换和国际交易中起着至关重要的作用。无论您是计划出国旅行、管理外部企业还是经营跨国贸易,获得实时货币兑换都是至关重要的。在本文中,我们将研究如何使用流行的 GUI 工具包 Python Tkinter 构建实时货币转换器。通过利用 Python 和 Tkinter 的强大功能,可以创建一个直观且用户友好的应用程序,它不仅可以执行精确的货币兑换,还可以为用户提供一个直观的界面来输入他们所需的金额并选择基础和目标货币。在本文中,我们将深入研究基本计算和分步使用,并展示满足不同需求的不同方法。
使用 Python Tkinter 的货币转换器
使用 Python Tkinter 进行实时货币转换包括创建一个图形用户界面 (GUI) 应用程序,允许用户即时转换货币。该应用程序遵循分步准备,从用户输入要转换的金额并选择基础货币和目标货币开始。为了提供实时汇率,该应用程序与货币汇率 API 互通。这可以使用"请求"库来完成,该库允许向 API 端点发出 HTTP 请求。API 返回一个 JSON 响应,其中包含不同货币的最新汇率。该应用程序从 API 响应中提取所选基础货币和目标货币之间的汇率。
方法
方法 1 - 使用 Web API 进行货币转换
方法 2 - 使用预加载的货币转换表
方法 3 - 使用 Python 库进行货币转换
方法 1:使用 Web API 进行货币转换
主要方法包括使用 Web API 获取实时货币汇率并执行转换。能够使用 Open Trade Rates 或 Exchange Rate-API 等知名 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 库。每种方法都有其优点,可以根据特定要求进行选择。通过遵循分步执行并理解所涉及的计算,用户可以轻松创建货币转换器应用程序并执行实时货币转换。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

