python 的 PyQT5 问题

我按照例子改写,改写结束不能正常运行。(原来的代码写在 后面)

以下是我的代码

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

global sec
sec = 0



class WorkThread(QThread):
    trigger = pyqtSignal()

    def __int__(self):
        super(WorkThread, self).__init__()

    def run(self):
        for i in range(2000000000):
            pass

        # 循环完毕后发出信号
        self.trigger.emit()

class MainTimer(QWidget):
    def __init__(self):
        super(MainTimer, self).__init__()

        self.initUI()
    def initUI(self):

        layout = QVBoxLayout(self)

        # 添加控件
        lcdNumber = QLCDNumber()
        layout.addWidget(lcdNumber)
        self.button = QPushButton("测试")
        layout.addWidget(self.button)
        # timer = QTimer()


        # 连接测试按钮和槽函数work

        self.timer = QTimer()
        self.workThread = WorkThread()

        self.button.clicked.connect(self.work)
        # 每次计时结束,触发setTime
        self.timer.timeout.connect(self.countTime)


        self.resize(300, 120)
        self.show()

    def work(self):
        # 计时器每秒计数
        self.timer.start(1000)
        # 计时开始
        self.workThread.start()
        # 当获得循环完毕的信号时,停止计数
        self.workThread.trigger.connect(self.timeStop)

    def countTime(self):
        global sec
        sec += 1
        # LED显示数字+1
        self.lcdNumber.display(sec)

    def timeStop(self):
        self.timer.stop()
        print("运行结束用时", self.lcdNumber.value())
        global sec
        sec = 0


if __name__ == "__main__":
    app = QApplication(sys.argv)
    top = MainTimer()

    # top.show()
    sys.exit(app.exec_())

下边是原来代码代码 :

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

global sec
sec = 0

# 增加了一个继承自QThread类的类,重新写了它的run()函数
# run()函数即是新线程需要执行的:执行一个循环;发送计算完成的信号。
class WorkThread(QThread):
    trigger = pyqtSignal()

    def __int__(self):
        super(WorkThread, self).__init__()

    def run(self):
        for i in range(2000000000):
            pass

        # 循环完毕后发出信号
        self.trigger.emit()


def countTime():
    global sec
    sec += 1
    # LED显示数字+1
    lcdNumber.display(sec)


def work():
    # 计时器每秒计数
    timer.start(1000)
    # 计时开始
    workThread.start()
    # 当获得循环完毕的信号时,停止计数
    workThread.trigger.connect(timeStop)


def timeStop():
    timer.stop()
    print("运行结束用时", lcdNumber.value())
    global sec
    sec = 0


if __name__ == "__main__":
    app = QApplication(sys.argv)
    top = QWidget()
    top.resize(300, 120)

    # 垂直布局类QVBoxLayout
    layout = QVBoxLayout(top)

    # 加个显示屏
    lcdNumber = QLCDNumber()
    layout.addWidget(lcdNumber)
    button = QPushButton("测试")
    layout.addWidget(button)

    timer = QTimer()
    workThread = WorkThread()

    button.clicked.connect(work)

    # 每次计时结束,触发 countTime
    timer.timeout.connect(countTime)

    top.show()
    sys.exit(app.exec_())



1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问