码迷,mamicode.com
首页 > 其他好文 > 详细

PyQt4多信号单槽的例子:计算复利

时间:2017-12-09 14:56:47      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:span   net   double   orm   markdown   ima   多个   cat   function   

代码

from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future_builtins import *

import sys
from PyQt4.QtCore import (SIGNAL)
from PyQt4.QtGui import (QApplication,
                         QComboBox,
                         QDialog,
                         QDoubleSpinBox,
                         QGridLayout,
                         QLabel)


class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        principalLabel = QLabel("Principal:")
        self.principalSpinBox = QDoubleSpinBox()
        self.principalSpinBox.setRange(1, 1000000000)
        self.principalSpinBox.setValue(1000)
        self.principalSpinBox.setPrefix("$ ")
        rateLabel = QLabel("Rate:")
        self.rateSpinBox = QDoubleSpinBox()
        self.rateSpinBox.setRange(1, 100)
        self.rateSpinBox.setValue(5)
        self.rateSpinBox.setSuffix(" %")
        yearsLabel = QLabel("Years:")
        self.yearsComboBox = QComboBox()
        self.yearsComboBox.addItem("1 year")
        self.yearsComboBox.addItems(["{0} years".format(x)
                                     for x in range(2, 26)])
        amountLabel = QLabel("Amount")
        self.amountLabel = QLabel()

        grid = QGridLayout()
        grid.addWidget(principalLabel, 0, 0)
        grid.addWidget(self.principalSpinBox, 0, 1)
        grid.addWidget(rateLabel, 1, 0)
        grid.addWidget(self.rateSpinBox, 1, 1)
        grid.addWidget(yearsLabel, 2, 0)
        grid.addWidget(self.yearsComboBox, 2, 1)
        grid.addWidget(amountLabel, 3, 0)
        grid.addWidget(self.amountLabel, 3, 1)
        self.setLayout(grid)

        self.connect(self.principalSpinBox,
                     SIGNAL("valueChanged(double)"), self.updateUi)
        self.connect(self.rateSpinBox,
                     SIGNAL("valueChanged(double)"), self.updateUi)
        self.connect(self.yearsComboBox,
                     SIGNAL("currentIndexChanged(int)"), self.updateUi)

        self.setWindowTitle("Interest")
        self.updateUi()

    def updateUi(self):
        """Calculates compound interest"""
        principal = self.principalSpinBox.value()
        rate = self.rateSpinBox.value()
        years = self.yearsComboBox.currentIndex() + 1
        amount = principal * ((1 + (rate / 100.0)) ** years)
        self.amountLabel.setText("$ {0:.2f}".format(amount))


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

运行效果:

技术分享图片

解析

多个信号为

self.connect(self.principalSpinBox,
             SIGNAL("valueChanged(double)"), self.updateUi)
self.connect(self.rateSpinBox,
             SIGNAL("valueChanged(double)"), self.updateUi)
self.connect(self.yearsComboBox,
             SIGNAL("currentIndexChanged(int)"), self.updateUi)

单个的槽函数为

def updateUi(self):
    """Calculates compound interest"""
    principal = self.principalSpinBox.value()
    rate = self.rateSpinBox.value()
    years = self.yearsComboBox.currentIndex() + 1
    amount = principal * ((1 + (rate / 100.0)) ** years)
    self.amountLabel.setText("$ {0:.2f}".format(amount))

PyQt4多信号单槽的例子:计算复利

标签:span   net   double   orm   markdown   ima   多个   cat   function   

原文地址:http://www.cnblogs.com/oneTOinf/p/8011367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!