|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2008-9 Qtrac Ltd. All rights reserved. |
| 3 | +# This program or module is free software: you can redistribute it and/or |
| 4 | +# modify it under the terms of the GNU General Public License as published |
| 5 | +# by the Free Software Foundation, either version 2 of the License, or |
| 6 | +# version 3 of the License, or (at your option) any later version. It is |
| 7 | +# provided for educational purposes and is distributed in the hope that |
| 8 | +# it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 9 | +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 10 | +# the GNU General Public License for more details. |
| 11 | + |
| 12 | +import sys |
| 13 | +from PyQt4.QtCore import (Qt, SIGNAL) |
| 14 | +from PyQt4.QtGui import (QApplication, QComboBox, QDialog, |
| 15 | + QDoubleSpinBox, QGridLayout, QLabel) |
| 16 | + |
| 17 | + |
| 18 | +class Form(QDialog): |
| 19 | + |
| 20 | + def __init__(self, parent=None): |
| 21 | + super(Form, self).__init__(parent) |
| 22 | + |
| 23 | + principalLabel = QLabel("Principal:") |
| 24 | + self.principalSpinBox = QDoubleSpinBox() |
| 25 | + self.principalSpinBox.setRange(1, 1000000000) |
| 26 | + self.principalSpinBox.setValue(1000) |
| 27 | + self.principalSpinBox.setPrefix("$ ") |
| 28 | + rateLabel = QLabel("Rate:") |
| 29 | + self.rateSpinBox = QDoubleSpinBox() |
| 30 | + self.rateSpinBox.setRange(1, 100) |
| 31 | + self.rateSpinBox.setValue(5) |
| 32 | + self.rateSpinBox.setSuffix(" %") |
| 33 | + yearsLabel = QLabel("Years:") |
| 34 | + self.yearsComboBox = QComboBox() |
| 35 | + self.yearsComboBox.addItem("1 year") |
| 36 | + self.yearsComboBox.addItems(["{0} years".format(x) |
| 37 | + for x in range(2, 26)]) |
| 38 | + amountLabel = QLabel("Amount") |
| 39 | + self.amountLabel = QLabel() |
| 40 | + |
| 41 | + grid = QGridLayout() |
| 42 | + grid.addWidget(principalLabel, 0, 0) |
| 43 | + grid.addWidget(self.principalSpinBox, 0, 1) |
| 44 | + grid.addWidget(rateLabel, 1, 0) |
| 45 | + grid.addWidget(self.rateSpinBox, 1, 1) |
| 46 | + grid.addWidget(yearsLabel, 2, 0) |
| 47 | + grid.addWidget(self.yearsComboBox, 2, 1) |
| 48 | + grid.addWidget(amountLabel, 3, 0) |
| 49 | + grid.addWidget(self.amountLabel, 3, 1) |
| 50 | + self.setLayout(grid) |
| 51 | + |
| 52 | + self.connect(self.principalSpinBox, |
| 53 | + SIGNAL("valueChanged(double)"), self.updateUi) |
| 54 | + self.connect(self.rateSpinBox, |
| 55 | + SIGNAL("valueChanged(double)"), self.updateUi) |
| 56 | + self.connect(self.yearsComboBox, |
| 57 | + SIGNAL("currentIndexChanged(int)"), self.updateUi) |
| 58 | + |
| 59 | + self.setWindowTitle("Interest") |
| 60 | + self.updateUi() |
| 61 | + |
| 62 | + |
| 63 | + def updateUi(self): |
| 64 | + """Calculates compound interest""" |
| 65 | + principal = self.principalSpinBox.value() |
| 66 | + rate = self.rateSpinBox.value() |
| 67 | + years = self.yearsComboBox.currentIndex() + 1 |
| 68 | + amount = principal * ((1 + (rate / 100.0)) ** years) |
| 69 | + self.amountLabel.setText("$ {0:.2f}".format(amount)) |
| 70 | + |
| 71 | + |
| 72 | +app = QApplication(sys.argv) |
| 73 | +form = Form() |
| 74 | +form.show() |
| 75 | +app.exec_() |
| 76 | + |
0 commit comments