Python——GUI編程 利息計算器 作業9(python programming)


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

class calMoney(QDialog):
    def __init__(self,parent=None):
        super().__init__(parent)

        self.setWindowTitle('帥帥的利息計算器')

        # 定義QLable時,在快捷鍵字母前加“&”符號;
        # alt+P
        PrincipalLabel = QLabel("&Principal:")
        self.inpMoney = QDoubleSpinBox()
        self.inpMoney.setPrefix("$ ") # 設置前綴
        self.inpMoney.setRange(0.01,100000000)
        self.inpMoney.setValue(1000)
        PrincipalLabel.setBuddy(self.inpMoney)

        RateLabel = QLabel("&Rate:")
        self.inpRate = QDoubleSpinBox()
        self.inpRate.setSuffix(" %") # 設置后綴
        self.inpRate.setValue(5)
        RateLabel.setBuddy(self.inpRate)

        YearsLabel = QLabel("&Years:")
        self.inpYears = QComboBox()
        ls=[]
        for i in range(1,11):
            if i==1:
                year = str(i) + " year"
            else:
                year = str(i) + " years"
            ls.append(year)
        self.inpYears.addItems(ls)
        YearsLabel.setBuddy(self.inpYears)

        AmountLabel = QLabel("&Amount")
        self.oupAmount = QLabel("$ 1102.50")
        AmountLabel.setBuddy(self.oupAmount)   

        # 網格布局
        layout = QGridLayout()
        layout.addWidget(PrincipalLabel, 0, 0)
        layout.addWidget(self.inpMoney, 0, 1)
        layout.addWidget(RateLabel, 1, 0)
        layout.addWidget(self.inpRate, 1, 1)
        layout.addWidget(YearsLabel, 2, 0)
        layout.addWidget(self.inpYears, 2, 1)
        layout.addWidget(AmountLabel, 3, 0)
        layout.addWidget(self.oupAmount, 3, 1)

        # 信號與槽相連
        self.inpMoney.valueChanged.connect(self.updateAmount)
        self.inpRate.valueChanged.connect(self.updateAmount)
        self.inpYears.currentIndexChanged.connect(self.updateAmount)

        self.setLayout(layout)

    def updateAmount(self):
        principal = float(self.inpMoney.value())
        rate = float(self.inpRate.value())
        years = int(self.inpYears.currentIndex())
        amount = principal * pow((1 + 0.01 * rate),(years+1))
        self.oupAmount.setText("{0:.2f}".format(amount))
        pass

app = QApplication(sys.argv)
form = calMoney()
form.show()
app.exec_()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM