0. 效果
image.png
1. UI文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'rate.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(371, 136)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 10, 351, 111))
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.label_6 = QtWidgets.QLabel(self.widget)
self.label_6.setObjectName("label_6")
self.gridLayout.addWidget(self.label_6, 0, 0, 1, 1)
self.base_doubleSpinBox = QtWidgets.QDoubleSpinBox(self.widget)
self.base_doubleSpinBox.setMaximum(100000000.0)
self.base_doubleSpinBox.setSingleStep(1000.0)
self.base_doubleSpinBox.setProperty("value", 10000.0)
self.base_doubleSpinBox.setObjectName("base_doubleSpinBox")
self.gridLayout.addWidget(self.base_doubleSpinBox, 0, 1, 1, 1)
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
self.rate_doubleSpinBox = QtWidgets.QDoubleSpinBox(self.widget)
self.rate_doubleSpinBox.setMaximum(100.0)
self.rate_doubleSpinBox.setProperty("value", 5.0)
self.rate_doubleSpinBox.setObjectName("rate_doubleSpinBox")
self.gridLayout.addWidget(self.rate_doubleSpinBox, 1, 1, 1, 1)
self.label_3 = QtWidgets.QLabel(self.widget)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
self.years_doubleSpinBox = QtWidgets.QDoubleSpinBox(self.widget)
self.years_doubleSpinBox.setMaximum(100.0)
self.years_doubleSpinBox.setProperty("value", 1.0)
self.years_doubleSpinBox.setObjectName("years_doubleSpinBox")
self.gridLayout.addWidget(self.years_doubleSpinBox, 2, 1, 1, 1)
self.label_4 = QtWidgets.QLabel(self.widget)
self.label_4.setObjectName("label_4")
self.gridLayout.addWidget(self.label_4, 3, 0, 1, 1)
self.total_label = QtWidgets.QLabel(self.widget)
self.total_label.setObjectName("total_label")
self.gridLayout.addWidget(self.total_label, 3, 1, 1, 1)
self.gridLayout.setColumnStretch(0, 1)
self.gridLayout.setColumnStretch(1, 5)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.base_doubleSpinBox.valueChanged['double'].connect(MainWindow.getResult)
self.rate_doubleSpinBox.valueChanged['double'].connect(MainWindow.getResult)
self.years_doubleSpinBox.valueChanged['double'].connect(MainWindow.getResult)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.base_doubleSpinBox, self.rate_doubleSpinBox)
MainWindow.setTabOrder(self.rate_doubleSpinBox, self.years_doubleSpinBox)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_6.setText(_translate("MainWindow", "本金"))
self.label_2.setText(_translate("MainWindow", "利率"))
self.label_3.setText(_translate("MainWindow", "年数"))
self.label_4.setText(_translate("MainWindow", "总额"))
self.total_label.setText(_translate("MainWindow", "0"))
2. 逻辑代码
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
from ui_rate import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.getResult(0)
def getResult(self,double):
base = self.ui.base_doubleSpinBox.value()
rate = self.ui.rate_doubleSpinBox.value()
year = self.ui.years_doubleSpinBox.value()
#print(total,type(total))
self.ui.total_label.setText(str(base*( (1+rate/100)**year) ))
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
网友评论