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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyQt4多信号单槽的例子:计算复利相关的知识,希望对你有一定的参考价值。

代码

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多信号单槽的例子:计算复利的主要内容,如果未能解决你的问题,请参考以下文章

Pyqt4学习笔记-事件和信号

Qt在多线程中使用信号槽的示例

Python Qt GUI设计:多线程中信号与槽的使用(基础篇—9)

QT中槽的使用

Qt信号和槽的问题

pyqt4在线程中向主线程中的插槽发出信号