Python GUI编程网 > PyQt5高级扩展应用 > QThread多线程使用方法
python教程

QThread多线程使用方法

发布日期:2021年4月26日 01:04
阅读:3570
作者:Python GUI编程网

QThread多线程使用方法

00:00 / 01:23
1x
2x
1.5x
1.25x
1x
0.8x
0.5x

QThread多线程使用方法

################################
# PyQt5中文网 - PyQt5全套视频教程 #
#    https://www.PyQt5.cn/     #
#         主讲: 村长            #
################################

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_())