Python Pyqt5多线程更新UI实例|防止界面卡死

python 源码巴士 639浏览

“””
在编写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实例|防止界面卡死