主页 > imtoken钱包新版下载 > python货币转换_简单的python制作货币转换器
python货币转换_简单的python制作货币转换器
在本文中,我们将构建一个令人兴奋的项目,您可以从中制作货币转换器。对于用户界面,我们将使用 tkinter 库。
Python中货币转换器的python构建步骤 货币转换器实时汇率的python构建步骤 导入所需库 CurrencyConverter类 货币转换器用户界面的主要功能
一、实时汇率
要获得实时汇率世界货币转换器,我们将使用:
基础 – 美元:这意味着我们有基础货币美元。这意味着要转换任何货币,我们必须先将其转换为美元,然后再从美元转换为任何货币。
日期和时间:显示上次更新的日期和时间。
汇率:这是基础货币与美元之间的货币汇率。
二、导入我们需要的库
我们使用 tkinter 和 request 库。因此,我们需要导入库。
import requestsfrom tkinter import *import tkinter as tkfrom tkinter import ttk
三、创建 CurrencyConverter 类
现在我们将创建 CurrencyConverter 类,该类将获取实时汇率并转换货币并返回转换后的金额。
1、让我们创建类的构造函数
class RealTimeCurrencyConverter(): def __init__(self,url): self.data = requests.get(url).json() self.currencies = self.data['rates']
requests.get(url) 将页面加载到我们的python程序中,然后.json()会将页面转换为json文件。我们将其存储在数据变量中。
2、Convert() 方法:
def convert(self, from_currency, to_currency, amount): initial_amount = amount if from_currency != 'USD' : amount = amount / self.currencies[from_currency] # limiting the precision to 4 decimal places amount = round(amount * self.currencies[to_currency], 4) return amount
此方法采用以下参数:
From_currency : 要转换的货币
to _currency:要转换为的货币
金额:要兑换的金额
并返回转换后的金额
例子:
url = 'https://api.exchangerate-api.com/v4/latest/USD'converter = RealTimeCurrencyConverter(url)print(converter.convert('CNY','USD',100))
100RMB=14.$7278
p>
四、我们为货币转换器创建一个 UI
要创建 UI,我们将创建 CurrencyConverterUI 类
def __init__(self, converter): tk.Tk.__init__(self) self.title = 'Currency Converter' self.currency_converter = converter
转换器:我们将用于转换货币的货币转换器对象。上面的代码将创建一个框架。
让我们创建转换器
self.geometry("500x200") # Label self.intro_label = Label(self, text = 'Welcome to Real Time Currency Convertor', fg = 'blue', relief = tk.RAISED, borderwidth = 3) self.intro_label.config(font = ('Courier',15,'bold')) self.date_label = Label(self, text = f"1 CNY = {self.currency_converter.convert('CNY','USD',1)} USD Date : {self.currency_converter.data['date']}", relief = tk.GROOVE, borderwidth = 5) self.intro_label.place(x = 10 , y = 5) self.date_label.place(x = 160, y= 50)
首先,我们设置框架并向其添加一些信息。执行完这部分代码后,我们的框架如下所示。
我们在框架中为货币数量和选项创建输入框。这允许用户输入金额并在货币之间进行选择。
# Entry box valid = (self.register(self.restrictNumberOnly), '%d', '%P') self.amount_field = Entry(self,bd = 3, relief = tk.RIDGE, justify = tk.CENTER,validate='key', validatecommand=valid) self.converted_amount_field_label = Label(self, text = '', fg = 'black', bg = 'white', relief = tk.RIDGE, justify = tk.CENTER, width = 17, borderwidth = 3) # dropdown self.from_currency_variable = StringVar(self) self.from_currency_variable.set("CNY") # default value self.to_currency_variable = StringVar(self) self.to_currency_variable.set("USD") # default value font = ("Courier", 12, "bold") self.option_add('*TCombobox*Listbox.font', font) self.from_currency_dropdown = ttk.Combobox(self, textvariable=self.from_currency_variable,values=list(self.currency_converter.currencies.keys()), font = font, state = 'readonly', width = 12, justify = tk.CENTER) self.to_currency_dropdown = ttk.Combobox(self, textvariable=self.to_currency_variable,values=list(self.currency_converter.currencies.keys()), font = font, state = 'readonly', width = 12, justify = tk.CENTER) # placing self.from_currency_dropdown.place(x = 30, y= 120) self.amount_field.place(x = 36, y = 150) self.to_currency_dropdown.place(x = 340, y= 120) #self.converted_amount_field.place(x = 346, y = 150) self.converted_amount_field_label.place(x = 346, y = 150)
注意:此代码是 __init__ 的一部分
代码执行成功后,我们这样显示。
现在,让我们添加 CONVERT 按钮,它将调用 perform 函数。
# Convert button self.convert_button = Button(self, text = "Convert", fg = "black", command = self.perform) self.convert_button.config(font=('Courier', 10, 'bold')) self.convert_button.place(x = 225, y = 135)
Command = self.perform - 表示单击时将调用 perform() 方法
perform() 方法:
perform 方法将接受用户输入并将金额转换为所需的货币并显示在 convert_amount 输入框中。
def perform(self): amount = float(self.amount_field.get()) from_curr = self.from_currency_variable.get() to_curr = self.to_currency_variable.get() converted_amount = self.currency_converter.convert(from_curr,to_curr,amount) converted_amount = round(converted_amount, 2) self.converted_amount_field_label.config(text = str(converted_amount))
注意:此函数是 App 类的一部分。
RestrictNumberOnly() 方法:
现在,让我们为输入框创建一个限制。因此,用户只能在“金额”字段中输入数字。我们前面已经讨论过,这将通过我们的 RretricNumberOnly 方法来完成。
def restrictNumberOnly(self, action, string): regex = re.compile(r"[0-9,]*?(.)?[0-9,]*$") result = regex.match(string) return (string == "" or (string.count('.') <= 1 and result is not None))
注意:这个函数是APP类的一部分
五、创建主函数
首先,我们将创建转换器。二、为Converter创建一个UI
if __name__ == '__main__': url = 'https://api.exchangerate-api.com/v4/latest/USD' converter = RealTimeCurrencyConverter(url) App(converter) mainloop()
在本文中世界货币转换器,我们使用 Python 项目来构建货币转换器设备。