pyqt5 编程2——复利计算器的编写

1、代码

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class Form(QDialog):
    """docstring for """
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        # self.

        self.toLabel1 = QLabel("Principal:")
        self.toLabel2 = QLabel("Rate:")
        self.toLabel3 = QLabel("Years:")
        self.toLabel4 = QLabel("Amount:")

        # self.fromSpinBox = QDoubleSpinBox()
        self.fromSpinBox1 = QDoubleSpinBox()
        self.fromSpinBox2 = QDoubleSpinBox()
        self.fromSpinBox3 = QDoubleSpinBox()

        self.fromSpinBox1.setRange(0.01, 10000000.00)
        self.fromSpinBox1.setValue(10000.0)
        self.fromSpinBox1.setPrefix("$ ")

        self.fromSpinBox2.setSuffix(" %")
        self.fromSpinBox2.setValue(4.20)

        self.fromSpinBox3.setSuffix(" years")
        self.fromSpinBox3.setValue(2)

        # principal=self.fromSpinBox1.value()
        # rate=self.fromSpinBox2.value()
        # years=self.fromSpinBox3.value()

        # amount=principal*((1+(rate/100.0))**years)

        # amount=1
        # self.toLabel5=QLabel(str(amount))

        button=QPushButton("Calculate",self)

        grid = QGridLayout()

        # grid.addWidget(dateLabel, 0, 0)

        # grid.addWidget(self.fromSpinBox, 1, 1)
        # grid.addWidget(self.toComboBox, 2, 0)
        grid.addWidget(self.toLabel1, 1, 0)
        grid.addWidget(self.toLabel2, 2, 0)
        grid.addWidget(self.toLabel3, 3, 0)
        grid.addWidget(self.toLabel4, 4, 0)
        # grid.addWidget(self.toLabel5, 4, 1)

        grid.addWidget(self.fromSpinBox1, 1, 1)
        grid.addWidget(self.fromSpinBox2, 2, 1)
        grid.addWidget(self.fromSpinBox3, 3, 1)
        # grid.addWidget(self.fromComboBox, 1, 1)
        grid.addWidget(button,5,0)

        toLabel5=QLabel("None")
        self.toLabel5=QLabel()

        # print(principal)
        button.clicked.connect(self.updateUi)


        grid.addWidget(self.toLabel5, 4, 1)
        self.setLayout(grid)

        self.setWindowTitle("interest")
        self.updateUi()
        self.initUI()

    def updateUi(self):

        principal=self.fromSpinBox1.value()
        rate=self.fromSpinBox2.value()
        years=self.fromSpinBox3.value()

        amount=principal*((1+(rate/100.0))**years)
        self.toLabel5.setText("$ %.2f"%amount)

    def initUI(self):
        # self.setGeometry(300, 200, 300, 200)
        # self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('logo.png'))
        self.show()

if __name__=="__main__":
    app=QApplication(sys.argv)
    form=Form()
    form.show()
    app.exec_()

猜你喜欢

转载自blog.csdn.net/rosefun96/article/details/79457305