您好,登錄后才能下訂單哦!
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代碼的實例分析問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。