wps2000 |
2016-10-18 23:00 |
自己写的第4章练习题interest,兼容PYQt5+python3
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import *
class Cpinterest(QDialog):
def __init__(self, parent=None): super(Cpinterest, self).__init__(parent) Principal = QLabel("Principal:") Rate = QLabel("Rate:") Years = QLabel("Years:") Amount = QLabel("Amount") self.Amount_value = QLabel() self.PrinSpinBox = QDoubleSpinBox() self.PrinSpinBox.setRange(2000.00, 10000000.00) self.PrinSpinBox.setSingleStep(100) self.PrinSpinBox.setValue(2000.00) self.PrinSpinBox.setPrefix("$") self.RateSpinBox = QDoubleSpinBox() self.RateSpinBox.setRange(1.00,200.00) self.RateSpinBox.setSingleStep(0.01) self.RateSpinBox.setValue(5.00) self.RateSpinBox.setSuffix("%") self.YearsComboBox = QComboBox() self.YearsComboBox.addItem("1 year") self.YearsComboBox.addItems(["%d years" % x for x in range(2, 100)]) grid = QGridLayout() grid.addWidget(Principal,0,0) grid.addWidget(self.PrinSpinBox,0,1) grid.addWidget(Rate,1,0) grid.addWidget(self.RateSpinBox,1,1) grid.addWidget(Years,2,0) grid.addWidget(self.YearsComboBox,2,1) grid.addWidget(Amount,3,0) grid.addWidget(self.Amount_value,3,1) self.setLayout(grid) self.PrinSpinBox.valueChanged.connect(self.updateUi) self.RateSpinBox.valueChanged.connect(self.updateUi) self.YearsComboBox.currentIndexChanged.connect(self.updateUi) self.setWindowTitle("Compound interest") self.updateUi()
def updateUi(self): year = self.YearsComboBox.currentIndex() + 1 amount = self.PrinSpinBox.value() * ((1 + (self.RateSpinBox.value() /100.0)) ** year) self.Amount_value.setText("$ %0.2f" % amount)
app = QApplication(sys.argv) interest = Cpinterest() interest.show() app.exec_()
|
|