• Pyqt-线程


    1、示例1

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from MainWidget import *
    
    class MainWidget(QWidget):
        def __init__(self, parent=None):
            super(MainWidget, self).__init__(parent)
            self.thread = Worker()
            self.listFile = QListWidget()
            self.btnStart = QPushButton('开始')
    
            layout = QGridLayout(self)
            layout.addWidget(self.listFile, 0, 0, 1, 2)
            layout.addWidget(self.btnStart, 1, 1)
    
            self.btnStart.clicked.connect(self.slotStart)   # 启动线程
            self.thread.sinOut.connect(self.slotAdd)        # 将信号连接到slotAdd,信号由线程每隔2s发送一次
    
        def slotAdd(self, file_list):
            self.listFile.addItem(file_list)
    
        def slotStart(self):
            self.btnStart.setEnabled(False)
            self.thread.start()
    
    # 新的线程:创建一个信号,该信号每隔2s发送一次
    # 线程启动后会自动开始执行run()函数
    class Worker(QThread):
        sinOut = pyqtSignal(str)    # 创建一个信号,信号必须在类创建时定义,不能在类创建后作为类的属性动态添加进来
    
        def __init__(self, parent=None):
            super(Worker, self).__init__(parent)
            self.working = True
            self.num = 0
        def __del__(self):
            self.working = False
            self.wait()
        def run(self):
            while self.working == True:
                file_str = 'File index{0}'.format(self.num)
                self.num += 1
                # 发射信号
                self.sinOut.emit(file_str)
                # 线程休眠2秒
                self.sleep(2)
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
    
        win = MainWidget()
        win.show()
    
        sys.exit(app.exec())

    运行结果:点下开始按钮,每隔2s添加一个条目

     2、示例2

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from MainWidget import *
    
    global sec
    sec = 0
    
    class MainWidget(QWidget):
        def __init__(self, parent=None):
            super(MainWidget, self).__init__(parent)
            self.widget = QWidget()
            self.widget.resize(300, 120)
    
            self.lcdNum = QLCDNumber()
            self.button = QPushButton("测试")
            self.timer = QTimer()
    
            layout = QVBoxLayout(self)
            layout.addWidget(self.lcdNum)
            layout.addWidget(self.button)
    
            self.button.clicked.connect(self.work)  #
            self.timer.timeout.connect(self.setTime)  # 定时任务,每隔100ms QLCDNumber值自加
    
        def setTime(self):
            global sec
            sec += 1
            self.lcdNum.display(sec)
    
    # 定义一个任务,该任务启动一个1000ms的定时器后开始一段非常耗时的操作(用for循环模拟)
        def work(self):
            self.timer.start(1000)
            for i in range(2000000000):  # 模拟一段非常耗时的计算,他会阻塞主线程app.exec(),从而让窗口停止响应
                pass
            self.timer.stop()
    
    
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
    
        win = MainWidget()
        win.show()
    
        sys.exit(app.exec())  # 这是主线程

    运行结果:点下测试按钮,程序不响应或者卡斯崩溃

     3、示例3

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from MainWidget import *
    
    global sec
    sec = 0
    
    class MainWidget(QWidget):
        def __init__(self, parent=None):
            super(MainWidget, self).__init__(parent)
            self.widget = QWidget()
            self.widget.resize(300, 120)
            # 控件
            self.lcdNum = QLCDNumber()
            self.button = QPushButton("测试")
            self.timer = QTimer()
            self.workThread = WorkThread()
            # 布局
            layout = QVBoxLayout(self)
            layout.addWidget(self.lcdNum)
            layout.addWidget(self.button)
            #信号与槽函数
            self.button.clicked.connect(self.work)  # 按钮按下触发: 定时器开启,新线程开启
            self.timer.timeout.connect(self.setTime)  # 每隔1000ms触发: QLCDNumber值自加
    
        def setTime(self):
            global sec
            sec += 1
            self.lcdNum.display(sec)
    
        def timeStop(self):
            self.timer.stop()
            print("运行结束用时", self.lcdNum.value())
            global sec
            sec = 0
    
        # 开启定时器和新线程
        def work(self):
            self.timer.start(1000)
            self.workThread.start()
            self.workThread.trigger.connect(self.timeStop)  # 线程结束时触发timeStop函数
    
    # 新的线程:模拟一段耗时的计算或其他操作(较长时间的死循环)
    class WorkThread(QThread):
        trigger = pyqtSignal()
    
        def __init__(self):
            super(WorkThread,self).__init__()
    
        def run(self):
            for i in range(400000000):  # 模拟一段非常耗时的计算,他会阻塞主线程app.exec(),从而让窗口停止响应
                pass
            self.trigger.emit()
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
    
        win = MainWidget()
        win.show()
    
        sys.exit(app.exec())  # 这是主线程

    运行结果:界面不会出现卡顿情况了,耗时计算完成后计数停止并print所用时间

  • 相关阅读:
    Android内存优化4 了解java GC 垃圾回收机制2 GC执行finalize的过程
    Android内存优化3 了解java GC 垃圾回收机制1
    Android内存优化2 了解java内存分配 2
    Android内存优化1 了解java内存分配 1
    Android Parcel对象详解
    Android中Parcel的分析以及使用
    Android进阶笔记:Messenger源码详解
    Android进阶笔记:AIDL内部实现详解 (二)
    Android进阶笔记:AIDL内部实现详解 (一)
    Android开发高级进阶——多进程间通信
  • 原文地址:https://www.cnblogs.com/Mike2019/p/13029485.html
Copyright © 2020-2023  润新知