15.3 PyQt5中QThread多線程使用


一、PyQt5中QThread多線程使用

1.基本概念

  • 首先創建所需要的線程,然后通過不同的線程對象實現不同的功能就可以了。

2.代碼

點擊查看代碼
from PyQt5.Qt import *
import sys
import time

# 1.重寫一個類
class Threads(QThread) :
    def __init__(self, *argv, **kwargs) :
        super().__init__(*argv, **kwargs)
    # 4.創建信號
    pic_thread = pyqtSignal()
    # 2.設置休眠時間
    def run(self) :
        time.sleep(2)
        self.pic_thread.emit() #5.接受信號,並發送信號


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) :
        label = QLabel(self)
        label.move(80, 80)
        label.resize(300, 250)
        label.setStyleSheet('background-color:green')
        self.label = label

        # 3.創建一個子線程
        thread = Threads(self)
        # 6.連接槽函數
        thread.pic_thread.connect(self.aaa)
        thread.start()  # 使用子線程執行run里面的代碼
    # 7.書寫槽函數
    def aaa(self) :
        pixmap = QPixmap('aaa.png')
        self.label.setPixmap(pixmap)
   
        # time.sleep(2)
        # pixmap = QPixmap('123.jpg')
        # label.setPixmap(pixmap)
  


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

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

3.效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM