Skip to content

Instantly share code, notes, and snippets.

@marodrig
Last active August 29, 2015 13:58
Show Gist options
  • Save marodrig/10299546 to your computer and use it in GitHub Desktop.
Save marodrig/10299546 to your computer and use it in GitHub Desktop.
Using PyQt to build a interest rate calculator GUI.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Form(QDialog):
def __init__(self, parent = None):
super(Form, self).__init__(parent)
#Labels
prin_label = QLabel("Principal:")
rate_label = QLabel("Rate:")
year_label = QLabel("Year(s):")
amount_label = QLabel("Interest total:")
self.result_label = QLabel()
value_label = QLabel("Monthly contribution:")
amount_label = QLabel("Annuity total:")
self.result_Label = QLabel()
total_label = QLabel("TOTAL:")
self.total_result = QLabel()
#Widgets
self.valueSpinBox = QDoubleSpinBox()
self.valueSpinBox.setRange(0.01, 1000000.00)
self.valueSpinBox.setValue(100)
self.valueSpinBox.setPrefix("$")
self.PrincipalSpinBox = QDoubleSpinBox()
self.PrincipalSpinBox.setRange(0.01, 1000000.00)
self.PrincipalSpinBox.setValue(100)
self.PrincipalSpinBox.setPrefix("$")
self.RateSpinBox = QDoubleSpinBox()
self.RateSpinBox.setRange(0.01, 100.00)
self.RateSpinBox.setValue(1)
self.RateSpinBox.setSuffix("%")
self.YearComboBox = QComboBox()
self.YearComboBox.addItem("1 year ")
for n in range (2, 11):
year = "%d years" % n
self.YearComboBox.addItem(year)
#Layout
grid = QGridLayout()
grid.addWidget(prin_label, 0, 0)
grid.addWidget(self.PrincipalSpinBox, 0, 1)
grid.addWidget(valueLabel, 1, 0)
grid.addWidget(self.valueSpinBox, 1, 1)
grid.addWidget(rate_label, 2, 0)
grid.addWidget(self.RateSpinBox, 2, 1)
grid.addWidget(year_label, 3, 0)
grid.addWidget(self.YearComboBox, 3, 1)
grid.addWidget(amount_label, 4, 0)
grid.addWidget(self.resultLabel, 4, 1)
grid.addWidget(amountLabel, 5, 0)
grid.addWidget(self.result_Label, 5, 1)
grid.addWidget(totalLabel, 6, 0)
grid.addWidget(self.totalresult, 6, 1)
self.setLayout(grid)
#Form behavior
self.valueSpinBox.valueChanged.connect(self.update_ui)
self.PrincipalSpinBox.valueChanged.connect(self.update_ui)
self.RateSpinBox.valueChanged.connect(self.update_ui)
self.YearComboBox.currentIndexChanged.connect(self.update_ui)
@pyqtSlot()
def update_ui(self):
principal = self.PrincipalSpinBox.value()
rate = self.RateSpinBox.value()
years = float(self.YearComboBox.currentIndex()) + 1
amount_int = principal *((1 + (rate/100.00))**years)
self.resultLabel.setText("${0:0.2f}".format(amount_int))
value = self.valueSpinBox.value()
i = (self.RateSpinBox.value()/100.00) / 12
n = years * 12
amount = ((1 + i)**n-1)*(value/i)
self.result_Label.setText("${0:0.2f}".format(amount))
total = amount + amount_int
self.totalresult.setText("${0:0.2f}".format(total))
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment