QThread:在线程仍在Python中运行时销毁
我有这部分代码,它有时有效,有时发出警告:QThread:在线程仍在Python中运行时销毁,python,pyqt,pyqt4,qthread,Python,Pyqt,Pyqt4,Qthread,我有这部分代码,它有时有效,有时发出警告: QThread:在线程仍在运行时销毁 此部件在UiMainWindow类中 obj_thread = QtCore.QThread() def ok(): self.module_src_tree.setStyleSheet('background-color: #81F781') obj_thread.quit() def err(): self.module_src_tree.setStyleSheet('backgrou
QThread:在线程仍在运行时销毁
此部件在UiMainWindow类中
obj_thread = QtCore.QThread()
def ok():
self.module_src_tree.setStyleSheet('background-color: #81F781')
obj_thread.quit()
def err():
self.module_src_tree.setStyleSheet('background-color: #F78181')
obj_thread.quit()
tmp = self.Temp(self, module_revision)
tmp.moveToThread(obj_thread)
tmp.finished.connect(ok)
tmp.error.connect(err)
obj_thread.started.connect(tmp.run)
obj_thread.start()
class Temp(QtCore.QObject):
finished = QtCore.pyqtSignal()
error = QtCore.pyqtSignal()
def __init__(self, gui, module_revision):
QtCore.QObject.__init__(self)
self.gui = gui
self.module_revision = module_revision
def run(self):
try:
self.gui.dp.pack_module_source(self.gui.module_src_show_list, self.gui.module_src_pack_list,
path=str(self.gui.path_box.text()), revision=self.module_revision)
self.finished.emit()
except Exception as e:
self.error.emit()
raise e
这是UiMainWindow类中的类
obj_thread = QtCore.QThread()
def ok():
self.module_src_tree.setStyleSheet('background-color: #81F781')
obj_thread.quit()
def err():
self.module_src_tree.setStyleSheet('background-color: #F78181')
obj_thread.quit()
tmp = self.Temp(self, module_revision)
tmp.moveToThread(obj_thread)
tmp.finished.connect(ok)
tmp.error.connect(err)
obj_thread.started.connect(tmp.run)
obj_thread.start()
class Temp(QtCore.QObject):
finished = QtCore.pyqtSignal()
error = QtCore.pyqtSignal()
def __init__(self, gui, module_revision):
QtCore.QObject.__init__(self)
self.gui = gui
self.module_revision = module_revision
def run(self):
try:
self.gui.dp.pack_module_source(self.gui.module_src_show_list, self.gui.module_src_pack_list,
path=str(self.gui.path_box.text()), revision=self.module_revision)
self.finished.emit()
except Exception as e:
self.error.emit()
raise e
我试图用这段代码做什么——我想在不冻结主应用程序的情况下压缩一些文件。所以我开始在后台工作的新线程。但我需要一种功能,即小部件在压缩完成后将其颜色更改为绿色,或者在出现问题时将其更改为红色。也许我做错了什么?也许不是这样吧?
我遇到的最大问题是改变颜色部分 致以最诚挚的问候,
马雷克
@三个菠萝,看起来有时候根本就不会开始。但是现在没有错误/警告。
我已将代码修改为:
class UiMainWindow(object):
# thread pool
thread_pool = [None, None, None, None]
这是在类构造函数之前。Temp类保持与上面相同,现在代码中的线程调用部分如下所示:
self.thread_pool[3] = None
self.thread_pool[3] = QtCore.QThread()
def ok():
self.module_src_tree.setStyleSheet('background-color: #81F781')
self.thread_pool[3].quit()
def err():
self.module_src_tree.setStyleSheet('background-color: #F78181')
self.thread_pool[3].quit()
tmp = self.Temp(self, module_revision)
tmp.moveToThread(self.thread_pool[3])
tmp.finished.connect(ok)
tmp.error.connect(err)
self.thread_pool[3].started.connect(tmp.run)
self.thread_pool[3].start()
当Python对线程进行垃圾收集时,就会出现此问题 您需要保存对
QThread
的引用,这样它就不会被垃圾收集。只需执行self.obj_thread=QtCore.QThread()
如果可能同时存在多个
QThread
s,并且将引用存储在同一个变量中,则可能需要将对象存储在列表中。但是,当给定的线程完成时,您需要从列表中清除对象(因此它们是垃圾收集的),这样您就不会给应用程序带来内存泄漏。将在明天检查这个问题,看看它是否有效。无论如何-谢谢你的帮助。我已经在主帖子中回答了。@jotto如果警告消失了,那么我想我已经修复了原始问题中的错误。如果线程现在不是一直在运行,那么您可能希望发布一个新问题,其中包含一个复制该问题的