📅  最后修改于: 2023-12-03 15:19:18.070000             🧑  作者: Mango
本文介绍了一个使用 Python 和 Tkinter 库编写的实时货币转换器。这个程序支持世界上所有主要的货币,可以让用户在它们之间进行转换。
该程序可以实现以下功能:
程序使用了 Tkinter 库来实现 GUI。用户可以通过 GUI 界面来输入要转换的货币金额,选择输入货币与输出货币的类型,然后点击“转换”按钮进行实时汇率转换。程序使用一些 API 来获取实时汇率信息,包括 Open Exchange Rates 和 Fixer。用户也可以选择把最近使用的货币对保存下来,以便在以后快速访问。
import tkinter as tk
import requests
class CurrencyConverter():
rates = {}
def __init__(self,url):
data = requests.get(url).json()
self.rates = data["rates"]
def convert(self, from_currency, to_currency, amount):
initial_amount = amount
if from_currency != 'USD' :
amount = amount / self.rates[from_currency]
amount = round(amount * self.rates[to_currency], 4)
return amount
class App(tk.Tk):
def __init__(self, converter):
tk.Tk.__init__(self)
self.title = "Currency Converter"
self.converter = converter
self.geometry('300x300')
self.configure(bg='white')
self.intro_label = tk.Label(self, text = '转换货币', fg='blue', font=('Arial', 15))
self.intro_label.place(x = 50, y = 20)
self.date_label = tk.Label(self,bg='white', text = f"1 USD = {self.converter.rates['USD']} USD")
self.date_label.place(x = 67, y= 65)
self.from_currency_variable = tk.StringVar(self)
self.from_currency_variable.set("USD") # default value
self.to_currency_variable = tk.StringVar(self)
self.to_currency_variable.set("EUR") # default value
self.amount_variable = tk.DoubleVar(self)
self.from_currency_dropdown = tk.OptionMenu(self, self.from_currency_variable, *self.converter.rates.keys())
self.to_currency_dropdown = tk.OptionMenu(self, self.to_currency_variable, *self.converter.rates.keys())
self.amount_entry = tk.Entry(self,textvariable=self.amount_variable)
self.from_currency_dropdown.place(x = 30, y= 100)
self.amount_entry.place(x = 36, y = 130)
self.to_currency_dropdown.place(x = 240, y= 100)
self.converted_amount_label = tk.Label(self, text = "", bg = 'white', fg = 'green', font = ('Arial', 10))
self.converted_amount_label.place(x = 90, y = 260)
self.convert_button = tk.Button(self, text = "转换", bg = 'green', fg = 'red', command = self.perform)
self.convert_button.place(x = 150, y = 135)
self.restart_button = tk.Button(self, text = "重置", bg = 'white', fg = 'red', command = self.reset)
self.restart_button.place(x = 70, y = 135)
self.close_button = tk.Button(self, text = "关闭", bg = 'red', fg = 'white', command = self.close_window)
self.close_button.place(x = 235, y = 135)
def perform(self):
amount = float(self.amount_variable.get())
from_curr = self.from_currency_variable.get()
to_curr = self.to_currency_variable.get()
converted_amount = self.converter.convert(from_curr,to_curr,amount)
converted_amount = round(converted_amount, 2)
self.converted_amount_label.config(text = str(converted_amount))
# save to history
self.history[str(from_curr) + ' to ' + str(to_curr)] = (amount, converted_amount)
def reset(self):
self.from_currency_variable.set("USD")
self.to_currency_variable.set("EUR")
self.amount_variable.set("")
self.converted_amount_label.config(text = "")
def close_window(self):
self.destroy()
if __name__ == '__main__':
url = 'https://api.exchangerate-api.com/v4/latest/USD'
converter = CurrencyConverter(url)
app = App(converter)
app.mainloop()
本文介绍了一个使用 Python 和 Tkinter 库编写的实时货币转换器。该程序能够支持所有主要货币之间的转换,提供实时汇率信息,支持保存最近使用的货币对。代码实现简单明了,可供 Python 程序员参考和学习。