问题描述
我正在 tkinter 上使用 if-else 语句编写货币转换器程序。我只针对六种货币编写了它,而且花费的时间太长。
如果我想写数百种货币怎么办?我也使用 if-else 吗?是否有更有效的方法来编程货币转换器而不使用 If-Else 语句?
from tkinter import ttk
from tkinter import *
import tkinter as tk
class CurrencyConverter(tk.Tk):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.title('Currency Converter')
self.geometry('500x300')
self.currencies_list = ['United States Dollar','Pound Sterling','Euro','Pakistani Rupee','Swiss Franc','Japanese Yen']
self.usd_to_other_list = [1,0.72,0.85,153.50,0.94,110.70]
self.ps_to_other_list = [1.38,1,1.18,212.15,1.3,153]
self.euro_to_other_list = [1.18,180.55,1.11,130.19]
self.pkr_to_other_list = [0.0065,0.0047,0.0055,0.0061,0.72]
self.sf_to_other_list = [1.06,0.77,0.9,162.79,117.46]
self.jy_to_other_list = [0.009,0.0065,0.0077,1.39,0.0085,1]
self.from_currency = tk.StringVar()
self.to_currency = tk.StringVar()
self.from_entry_variable = tk.Intvar()
self.to_entry_variable = tk.Intvar()
self.from_entry = ttk.Entry(
self,textvariable=self.from_entry_variable,width=40
)
self.from_comboBox = ttk.ComboBox(
self,textvariable=self.from_currency,values=tuple(self.currencies_list),state='readonly'
)
self.from_comboBox.set(self.currencies_list[0])
self.to_entry = ttk.Entry(
self,textvariable=self.to_entry_variable,width=40,state='readonly'
)
self.to_comboBox = ttk.ComboBox(
self,textvariable=self.to_currency,state='readonly'
)
self.to_comboBox.set(self.currencies_list[4])
self.from_entry.pack()
self.from_comboBox.pack()
self.to_entry.pack()
self.to_comboBox.pack()
self.from_entry.bind('<KeyPress>',self.currency_converter)
self.from_entry.bind('<keyrelease>',self.currency_converter)
def currency_converter(self,event):
try:
if self.from_currency.get() == 'United States Dollar':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.usd_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
elif self.from_currency.get() == 'Pound Sterling':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.ps_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
elif self.from_currency.get() == 'Euro':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.euro_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
elif self.from_currency.get() == 'Pakistani Rupee':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.pkr_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
elif self.from_currency.get() == 'Swiss Franc':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.sf_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
elif self.from_currency.get() == 'Japanese Yen':
if self.to_currency.get() == 'United States Dollar':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[0]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pound Sterling':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[1]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Euro':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[2]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Pakistani Rupee':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[3]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Swiss Franc':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[4]
self.to_entry_variable.set(value=f'{converted}')
elif self.to_currency.get() == 'Japanese Yen':
converted = int(self.from_entry_variable.get()) * self.jy_to_other_list[5]
self.to_entry_variable.set(value=f'{converted}')
except TclError:
pass
root = CurrencyConverter()
root.mainloop()
解决方法
像这样的一些函数(我没有 Python 编译器 - 可能存在小错误)?
currencies_list = ['United States Dollar','Pound Sterling','Euro','Pakistani Rupee','Swiss Franc','Japanese Yen']
usd_to_other_list = [1,0.72,0.85,153.5,0.94,110.7]
def GetParity(ValueToConvert,FromCurrency,ToCurrency):
SourceIndex = currencies_list.Index(FromCurrency)
DestIndex = currencies_list.Index(ToCurrency)
SourceR = usd_to_other_list(SourceIndex)
DestR = usd_to_other_list(DestIndex)
Result = ValueToConvert * DestR / SourceR
Return(Result)
,
所以我设法使用字典对货币转换器进行了编程。使用 if-else 的缺点是您会在所有汇率中忙于输入数百个嵌套的 if-else。
说明
首先,我们需要有一种规范货币或基础货币,所有其他货币都与它进行比较 (from_currency -> base_currency -> to_currency)
。然后你可以有一个字典将每种货币映射到一个单一的汇率,它的汇率与规范货币的关系(在规范货币本身的情况下,该汇率将为 1.0)。
此答案的解释由@jonrsharpe 完成。
算法
假设我想从 EUR
转换为 AED
。
第 1 步:设置基础/规范货币 (base currency: USD)
。
第 2 步:将 EUR
转换为 USD
。
第 3 步:将 USD
转换为 AED
。
实施
from tkinter import ttk
import tkinter as tk
from functools import partial
class CurrencyConverter(tk.Tk):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.title('Currency Converter')
self.geometry('500x300')
# Base currency: USD
self.rates = {
'USD': 1,'EUR': 0.85,'AED': 3.67
}
self.currencies_list = ['USD','EUR','AED']
self.from_currency = tk.StringVar()
self.to_currency = tk.StringVar()
self.from_entry_variable = tk.IntVar()
self.to_entry_variable = tk.IntVar()
self.from_entry = ttk.Entry(
self,textvariable=self.from_entry_variable,width=40
)
self.from_combobox = ttk.Combobox(
self,textvariable=self.from_currency,values=tuple(self.currencies_list),state='readonly'
)
self.from_combobox.set('USD')
self.to_entry = ttk.Entry(
self,textvariable=self.to_entry_variable,width=40,state='readonly'
)
self.to_combobox = ttk.Combobox(
self,textvariable=self.to_currency,state='readonly'
)
self.to_combobox.set('EUR')
self.button = ttk.Button(
self,text='Convert',command=partial(self.currency_converter,self.from_entry_variable,self.from_currency,self.to_currency)
)
self.from_entry.pack()
self.from_combobox.pack()
self.to_entry.pack()
self.to_combobox.pack()
self.button.pack()
所以,这是我的 currency_converter()
。我在这里传递 *args
是因为我想采用三个参数 amount
、from_currency
、to_currency
。
def currency_converter(self,*args):
_amount,_from_currency,_to_currency,= args
amount,from_currency,to_currency = _amount.get(),_from_currency.get(),_to_currency.get()
print(f'Amount: {amount},From: {from_currency},To: {to_currency}')
if from_currency != 'USD':
# Suppose I want to convert from EUR to AED.
# Convert EUR to USD.
usd = amount / self.rates[from_currency]
# Convert USD to AED.
amount = usd * self.rates[to_currency]
print(amount)
else:
# In the case of the canonical currency itself,that rate will be 1.0
amount = amount * self.rates[to_currency]
print(amount)
root = CurrencyConverter()
root.mainloop()
注意:我只针对三种货币完成了这项工作,我的规范货币是美元。如果您想添加更多货币,只需将它们以美元汇率添加到 self.rates
。