15.3 PyQt5中QThread多執行緒使用
阿新 • • 發佈:2022-04-20
一、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_())