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_()