Any仅允许从 Qt 主线程内访问 UI 元素。注意access不仅意味着读/写小部件属性,还意味着创建;从其他线程执行此操作的任何尝试都会在最好的情况下导致图形问题或不一致的行为,并在最坏(也是更常见)的情况下导致崩溃。
唯一正确的方法是使用带有(可能)自定义信号的 QThread:这允许 Qt 正确地对信号进行排队,并在实际可以处理它们时对它们做出反应。
下面是一个very简单的情况,不需要创建 QThread 子类,但请考虑这只是为了教育性的目的。
class Ui_MainWindow(object):
# ...
def calculation(self):
for i in range(10):
time.sleep(1)
print(i)
def showMessage(self):
msg = QtWidgets.QMessageBox()
msg.setInformativeText('Finish')
msg.exec_()
self.pushButton.setEnabled(True)
def threadingc(self):
self.pushButton.setEnabled(False)
self.thread = QtCore.QThread()
# override the `run` function with ours; this ensures that the function
# will be executed in the new thread
self.thread.run = self.calculation
self.thread.finished.connect(self.showMessage)
self.thread.start()
请考虑以下重要方面:
- 我必须禁用该按钮,否则可能会在前一个线程仍在执行的同时创建一个新线程;这会产生一个问题,因为覆盖
self.thread
将导致 python 尝试垃圾收集(delete) 运行时的前一个线程,这是一个very bad thing;
- 一个可能的解决方案是创建具有父线程的线程,这通常通过简单的方法完成
QThread(self)
,但这在您的情况下是不可能的,因为 Qt 对象只能接受其他 Qt 对象作为其父对象,而在您的情况下self
将是一个Ui_MainWindow
实例(这是一个基本的Python对象);
- 上述一点是一个重要的问题,因为您试图从
pyuic
生成的文件,应该never完成:这些文件应该保持原样any手动修改,仅作为导入模块使用;在官方指南中阅读有关此主题的更多信息使用设计器 https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html;还要注意,尝试模仿这些文件的行为是没有用的,因为通常会导致对对象结构的极大混乱;
- 理论上你可以添加对 qt 对象的引用(例如,通过添加
self.mainWindow = MainWindow
in the setupUi()
函数)并使用该引用创建线程(thread = QThread(self.mainWindow)
),或者将线程添加到持久列表(self.threads = []
,再次在setupUi()
),但由于上述几点,我强烈建议您不要这样做;
最后,还有一个correct代码的实现需要您再次生成 ui 文件,保持原状并执行类似以下示例的操作;请注意,我添加了一个非常基本异常实现还展示了如何正确与自定义信号交互。
from PyQt5 import QtCore, QtGui, QtWidgets
from mainwindow import Ui_MainWindow
import time
class Calculation(QtCore.QThread):
error = QtCore.pyqtSignal(object)
def run(self):
for i in range(10):
time.sleep(1)
print(i)
try:
10 / 0
except Exception as e:
self.error.emit(e)
break
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.pressed.connect(self.threadingc)
def showMessage(self):
msg = QtWidgets.QMessageBox()
msg.setInformativeText('Finish')
msg.exec_()
def threadingc(self):
# create the thread with the main window as a parent, this is possible
# since QMainWindow also inherits from QObject, and this also ensures
# that python will not delete it if you want to start another thread
thread = Calculation(self)
thread.finished.connect(self.showMessage)
thread.error.connect(self.showError)
thread.start()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
在上述情况下,使用以下命令处理 ui 文件(显然,假设 ui 名为“mainwindow.ui”):
pyuic mainwindow.ui -o mainwindow.py