from __future__ import division
'''
该实例是一个利率计算小程序,用来计算用户存款
在指定年限与利率的情况,用户获取的总金额。
界面设计:
QDialog中包含一个QVBoxLayout、4个QHBoxLayout
两个范围器(QDoubleSpinBox)、
一个下拉框(QComboBox)、
五个QLabel用来显示相关信息。
用户金额计算公式:
amount = principal * ((1 + (rate / 100.0)) ** years)
principal:表示初始存款金额
rate:表示利率
years:存款的年限
amount:指定年限内获取的总金额
'''
import math
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class InterestForm(QDialog):
def __init__(self,parent=None):
super(InterestForm,self).__init__(parent)
mvblayout=QVBoxLayout()
principalLabel=QLabel("Principal:")
self.principalQdspinbox=QDoubleSpinBox()
self.principalQdspinbox.setRange(0.00,1000000000.00)
self.principalQdspinbox.setPrefix("$")
self.principalQdspinbox.setValue(500.00)
prinHbly=QHBoxLayout()
prinHbly.addWidget(principalLabel)
prinHbly.addWidget(self.principalQdspinbox)
rateLabel=QLabel("Rate:")
self.rateQdspinbox=QDoubleSpinBox()
self.rateQdspinbox.setRange(0.35,10)
self.rateQdspinbox.setSuffix("%")
rateHbly=QHBoxLayout()
rateHbly.addWidget(rateLabel)
rateHbly.addWidget(self.rateQdspinbox)
yearsLabel=QLabel("Years:")
self.yearsCombox=QComboBox()
yearslist=[]
for year in range(1,100):
year="%d years" %year
yearslist.append(year)
self.yearsCombox.addItems(yearslist)
yearsHbly=QHBoxLayout()
yearsHbly.addWidget(yearsLabel)
yearsHbly.addWidget(self.yearsCombox)
amountLabel=QLabel("Amount:")
self.amountShowLabel=QLabel("$ 0.00")
amountHbly=QHBoxLayout()
amountHbly.addWidget(amountLabel)
amountHbly.addWidget(self.amountShowLabel)
mvblayout.addLayout(prinHbly)
mvblayout.addLayout(rateHbly)
mvblayout.addLayout(yearsHbly)
mvblayout.addLayout(amountHbly)
self.connect(self.principalQdspinbox,SIGNAL("valueChanged(double)"),self.updeatAmount)
self.connect(self.rateQdspinbox,SIGNAL("valueChanged(double)"),self.updeatAmount)
self.connect(self.yearsCombox,SIGNAL("currentIndexChanged(int)"),self.updeatAmount)
self.setLayout(mvblayout)
self.setWindowTitle("Interest")
def updeatAmount(self):
try:
principal=self.principalQdspinbox.value()
rate=self.rateQdspinbox.value()
years=self.yearsCombox.currentIndex()+1
amount=principal * ((1 + (rate / 100.0)) ** years)
self.amountShowLabel.setText("$ %.2f" %amount)
except Exception,e:
self.amountShowLabel.setText("The Error: %s" %e)
if __name__=="__main__":
app=QApplication(sys.argv)
interest=InterestForm()
interest.show()
app.exec_()