当前位置: 首页 PyQt5扩展功能大全 PyQt5中QThread多线程使用
add-vip

PyQt5中QThread多线程使用

一、学习指导

在PyQt5项目开发过程中我们经常会遇到线程方面的问题和需求,在这里为大家演示一下多线程的使用方法,和Python中使用是差不多的,首先创建所需要的线程,然后通过不同的线程对象实现不同的功能就可以了。

二、代码演示


from PyQt5.Qt import *
import sys
import time


class Threads(QThread):
    def __init__(self, *argv, **kwargs):
        super().__init__(*argv, **kwargs)

    pic_thread = pyqtSignal()

    def run(self):
        time.sleep(2)
        self.pic_thread.emit()


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("多线程 - PyQt5中文网")
        self.resize(600, 500)
        self.func_list()

    def func_list(self):
        self.func()

    def func(self):
        btn = QPushButton('多线程', self)
        btn.move(30, 30)

        label = QLabel(self)
        label.move(80, 80)
        label.resize(300, 250)
        label.setStyleSheet('background-color:green')
        self.label = label

        thread = Threads(self)  # 创建一个子线程
        thread.pic_thread.connect(self.aaa)
        thread.start()  # 使用子线程执行run里面的代码

    def aaa(self):
        pixmap = QPixmap('123.jpg')
        self.label.setPixmap(pixmap)

        # time.sleep(2)
        # pixmap = QPixmap('123.jpg')
        # label.setPixmap(pixmap)
        # print('WWWW')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()

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


相关文章