pyqt5的实验下自定义信号

2018-04-03  本文已影响0人  宋森_8f0b
import sys
import time
import pendulum
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5.QtWidgets import *

class Example(QWidget):
    valueChanged = QtCore.pyqtSignal(int)
    def __init__(self):
        super(Example, self).__init__()
    
        self.value = 0
    
        self.showLabel = QLabel('TEST', self)
        self.btnStart = QPushButton('START')
        self.hBox = QHBoxLayout()
    
        self.hBox.addWidget(self.showLabel)
        self.hBox.addWidget(self.btnStart)

        self.setLayout(self.hBox)

        self.valueChanged.connect(self.changeValue)
        self.btnStart.clicked.connect(self.testStart)

        # 模拟数值改变
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(lambda: self.setValue(self.value + 1))

    def changeValue(self, value):
        self.showLabel.setText(str(value))

    def setValue(self, v):
        self.value = v
        self.valueChanged.emit(self.value)
        print("I'm running@", pendulum.now())

    def testStart(self):
        self.setValue(self.value)
        self.timer.start(500)

    def closeEvent(self, event):
        self.timer.timeout.disconnect()
        print("I'm stop@", pendulum.now())
        event.accept()
        
if name == "main":
    app = QApplication(sys.argv)
    w = Example()
    w.show()
    # app.exec_()
    # sys.exit(0)
    sys.exit(app.exec_())
上一篇 下一篇

猜你喜欢

热点阅读