Python Pyqt5多線程更新UI代碼實例(防止界面卡死)
“””
在編寫GUI界面中,通常用會有一些按鈕,點擊後觸發事件,
比如去下載一個文件或者做一些操作,
這些操作會耗時,如果不能及時結束,主線程將會阻塞,
這樣界面就會出現未響應的狀態,因此必須使用多線程來解決這個問題。
“””
代碼實例
from PyQt5.Qt import (QApplication, QWidget, QPushButton,QThread,QMutex,pyqtSignal) import sys import time qmut_1 = QMutex() # 創建線程鎖 qmut_2 = QMutex() qmut_3 = QMutex() # 繼承QThread class Thread_1(QThread): # 線程1 def __init__(self): super().__init__() def run(self): qmut_1.lock() # 加鎖 values = [1, 2, 3, 4, 5,6,7,8,9,10] print("==== Thread_1 ====") for i in values: print("Thread_1:",i) time.sleep(0.5) # 休眠 qmut_1.unlock() # 解鎖 class Thread_2(QThread): # 線程2 _signal =pyqtSignal() def __init__(self): super().__init__() def run(self): # qmut_2.lock() # 加鎖 values = ["a", "b", "c", "d", "e","f","g","h","i","j","k"] print("==== Thread_2 ====") for i in values: print("Thread_2:",i) time.sleep(0.5) # qmut_2.unlock() # 解鎖 self._signal.emit() class Thread_3(QThread): # 線程2 _signal =pyqtSignal() def __init__(self): super().__init__() def run(self): qmut_3.lock() # 加鎖 values = ["a", "b", "c", "d", "e","f","g","h","i","j","k"] print("==== Thread_3 ====") for i in values: print("Thread_3:",i) time.sleep(0.5) qmut_3.unlock() # 解鎖 self._signal.emit() #執行完畢後,釋放信號 class Thread_01(QThread): # 線程1 def __init__(self): super().__init__() def run(self): values = [1, 2, 3, 4, 5] print("==== Thread_01 ====") for i in values: print("Thread_01:",i) time.sleep(0.5) # 休眠 class Thread_02(QThread): # 線程2 def __init__(self): super().__init__() def run(self): values = ["a", "b", "c", "d", "e"] print("==== Thread_02 ====") for i in values: print("Thread_02:",i) time.sleep(0.5) class MyWin(QWidget): def __init__(self): super().__init__() # 按鈕初始化 self.btn_01 = QPushButton('按鈕_每點一次運行一次', self) self.btn_01.move(80, 40) self.btn_01.clicked.connect(self.click_01) # 綁定槽函數 self.btn_02 = QPushButton('按鈕_每點一次運行一次', self) self.btn_02.move(80, 80) self.btn_02.clicked.connect(self.click_02) # 綁定槽函數 self.btn_1 = QPushButton('按鈕_線程鎖_多次點擊,依次執行(滯後感)', self) self.btn_1.move(80, 120) self.btn_1.clicked.connect(self.click_1) # 綁定槽函數 self.btn_2 = QPushButton('按鈕_線程鎖_收到完成信號後才能再次點擊', self) self.btn_2.move(80, 160) self.btn_2.clicked.connect(self.click_2) # 綁定槽函數 self.btn_3 = QPushButton('按鈕_線程鎖_收到完成信號後再次執行', self) self.btn_3.move(80, 200) self.btn_3.clicked.connect(self.click_3) # 綁定槽函數 def click_01(self): self.thread_01 = Thread_01() # 創建線程 self.thread_01.start() # 開始線程 def click_02(self): self.thread_02 = Thread_02() # 創建線程 self.thread_02.start() # 開始線程 def click_1(self): self.thread_1 = Thread_1() # 創建線程 self.thread_1.start() # 開始線程 def click_2(self): self.btn_2.setEnabled(False) self.thread_2 = Thread_2() self.thread_2._signal.connect(self.set_btn_2) #信號連接,如果收到信號,就執行對應的函數 self.thread_2.start() def click_3(self): self.btn_3.setEnabled(False) self.thread_3 = Thread_3() self.thread_3._signal.connect(self.set_btn_3) #信號連接,如果收到信號,就執行對應的函數 self.thread_3.start() def set_btn_2(self): self.btn_2.setEnabled(True) def set_btn_3(self): self.btn_3.setEnabled(True) if __name__ == "__main__": app = QApplication(sys.argv) myshow = MyWin() myshow.setWindowTitle("多線程演示") myshow.setMinimumHeight(500) myshow.setMinimumWidth(500) myshow.show() sys.exit(app.exec_())
運行結果
==== Thread_01 ====
Thread_01: 1
Thread_01: 2
Thread_01: 3
Thread_01: 4
Thread_01: 5
==== Thread_02 ====
Thread_02: a
Thread_02: b
Thread_02: c
Thread_02: d
Thread_02: e
==== Thread_1 ====
Thread_1: 1
Thread_1: 2
Thread_1: 3
Thread_1: 4
Thread_1: 5
Thread_1: 6
Thread_1: 7
Thread_1: 8
Thread_1: 9
==== Thread_2 ====
Thread_2: a
Thread_1: 10
Thread_2: b
Thread_2: c
Thread_2: d
==== Thread_3 ====
Thread_3: a
Thread_2: e
Thread_3: b
Thread_2: f
Thread_3: c
Thread_2: g
Thread_3: d
Thread_2: h
Thread_3: e
Thread_2: i
Thread_3: f
Thread_2: j
Thread_3: g
Thread_2: k
Thread_3: h
運行過程
到此這篇關於Python Pyqt5多線程更新UI代碼實例(防止界面卡死)的文章就介紹到這瞭,更多相關Python Pyqt5多線程更新U內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- QT5 Thread線程的具體實現
- Pyqt5 實現多線程文件搜索的案例
- Python+PyQT5實現手繪圖片生成器
- Python PyQt5實戰項目之查詢器的實現流程詳解
- 利用PyQt5制作一個豆瓣電影信息查看器