前言
- 在pyqt5中,gui线程通常指的是qt的主事件循环线程,也称为主线程。主线程负责处理gui事件、更新ui界面等任务。在pyqt5中,主线程和gui线程是同一个线程,即运行应用程序的线程。
- 当创建一个qt应用程序时,主线程会启动,并执行qapplication.exec_()方法,进入qt的事件循环。在事件循环中,主线程会不断地监听并处理用户的输入事件、定时器事件、网络事件等,然后更新ui界面。
- 如果在主线程执行耗时操作,比如
循环、sleep、wait 异步线程执行
会导致 ui 界面进入无响应状态,我们可以采用以下两种方式异步处理:使用qthread 或 qtimer
。
版本
- pyqt5
- python 3.x
案例
- 我们写一个简单的进度条填充程序,每 2 秒填充 1%:
import sys import time from pyqt5.qtwidgets import qapplication, qwidget, qprogressbar, qpushbutton, qhboxlayout class mywidget(qwidget): def __init__(self): super(mywidget, self).__init__() self.currentvalue = 0 self.progressbar = qprogressbar(self) self.progressbar.resize(200, 50) self.progressbar.move(20, 20) self.progressbar.setvalue(self.currentvalue) # 创建一个按钮 self.button = qpushbutton('点击我', self) self.button.clicked.connect(self.on_clicked) # 创建一个垂直布局,并将按钮添加到布局中 layout = qhboxlayout() layout.addwidget(self.progressbar) layout.addwidget(self.button) # 设置窗口的主布局为垂直布局 self.setlayout(layout) def on_clicked(self): while true: time.sleep(2) self.currentvalue = (self.currentvalue + 1) % 101 self.progressbar.setvalue(self.currentvalue) if __name__ == '__main__': app = qapplication(sys.argv) w = mywidget() w.resize(500, 300) w.move(300, 300) w.setwindowtitle('simple') w.show() sys.exit(app.exec_())
- 点击运行,我们会发现 ui 界面出现无响应且进度条没有刷新:
解决方案
- 为了避免 ui 界面无响应,我们可以采用以下两种方式:使用
qthread 或 qtimer
。
qthread
- 我们可以通过点击事件创建
qthread
异步线程执行:
import sys import time from pyqt5.qtcore import qthread, pyqtsignal from pyqt5.qtwidgets import qapplication, qwidget, qprogressbar, qpushbutton, qhboxlayout class myworker(qthread): timeout = pyqtsignal() def __init__(self): super(myworker, self).__init__() def run(self): while true: time.sleep(2) self.timeout.emit() class mywidget(qwidget): def __init__(self): super(mywidget, self).__init__() self.worker = none self.currentvalue = 0 self.progressbar = qprogressbar(self) self.progressbar.resize(200, 50) self.progressbar.move(20, 20) self.progressbar.setvalue(self.currentvalue) # 创建一个按钮 self.button = qpushbutton('点击我', self) self.button.clicked.connect(self.on_clicked) # 创建一个垂直布局,并将按钮添加到布局中 layout = qhboxlayout() layout.addwidget(self.progressbar) layout.addwidget(self.button) # 设置窗口的主布局为垂直布局 self.setlayout(layout) def on_clicked(self): self.worker = myworker() self.worker.timeout.connect(self.upgradeprogress) self.worker.start() def upgradeprogress(self): self.currentvalue = (self.currentvalue + 1) % 101 self.progressbar.setvalue(self.currentvalue) if __name__ == '__main__': app = qapplication(sys.argv) w = mywidget() w.resize(500, 300) w.move(300, 300) w.setwindowtitle('simple') w.show() sys.exit(app.exec_())
运行效果:
qtimer
- 我们可以通过点击事件创建
qtimer
定时器异步执行:
import sys from pyqt5.qtcore import qtimer from pyqt5.qtwidgets import qapplication, qwidget, qprogressbar, qpushbutton, qhboxlayout class mywidget(qwidget): def __init__(self): super(mywidget, self).__init__() self.currentvalue = 0 self.progressbar = qprogressbar(self) self.progressbar.resize(200, 50) self.progressbar.move(20, 20) self.progressbar.setvalue(self.currentvalue) # 创建一个按钮 self.button = qpushbutton('点击我', self) self.button.clicked.connect(self.on_clicked) # 创建一个垂直布局,并将按钮添加到布局中 layout = qhboxlayout() layout.addwidget(self.progressbar) layout.addwidget(self.button) # 设置窗口的主布局为垂直布局 self.setlayout(layout) def on_clicked(self): # 定义一个定时器并启动定时器 self.time = qtimer() self.time.timeout.connect(self.upgradeprogress) self.time.start(200) def upgradeprogress(self): self.currentvalue = (self.currentvalue + 1) % 101 self.progressbar.setvalue(self.currentvalue) if __name__ == '__main__': app = qapplication(sys.argv) w = mywidget() w.resize(500, 300) w.move(300, 300) w.setwindowtitle('simple') w.show() sys.exit(app.exec_())
- 运行效果:
局部变量创建异步线程导致 ui 未响应
- 在使用
qthread
的案例中,将on_clicked
方法改为如下写法,同样会导致 ui 未响应状态:
def on_clicked(self): worker = myworker() worker.timeout.connect(self.upgradeprogress) worker.start()
- 这是因为在python中,类似于
worker = myworker()
这样的语句创建的对象在当前作用域中是局部变量,它的生命周期与当前作用域相关联。当当前作用域的代码执行完成后局部变量会被销毁。 - 如果异步线程的任务还没有完成,而主线程的事件循环又需要等待任务完成才能继续执行,那么就会导致gui线程无响应。这是因为主线程被阻塞在等待异步任务的过程中,无法处理事件。
- 为了避免这种情况,我们应该将异步线程对象存储为实例变量(即使用
self.worker = myworker()
),这样可以确保异步线程对象的生命周期与主对象相同,直到异步任务完成。这样即使当前作用域的代码执行完成,异步线程仍然可以继续执行,并且主线程的事件循环也不会被阻塞。
如果 qtimer 不使用 self.time 写法
- 同理,如果不使用
self.time
写法,会被当做当前作用域中的局部变量,当前作用域代码执行完成后就会被销毁,不再继续执行。
到此这篇关于pyqt5界面无响应的解决方案的文章就介绍到这了,更多相关pyqt5界面无响应内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论