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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 花不完的钱可以打这里