【发布时间】:2018-08-09 14:37:35
【问题描述】:
尝试编写一个显示线程进程进度的类。我需要将此类用于所有“文件加载”操作;但是我在使其全球化时遇到了麻烦。
文件加载器.py:
from PyQt5.QtCore import pyqtSlot, pyqtSignal
from PyQt5.QtWidgets import QDialog
from PyQt5.uic import loadUi
class FileLoader(QDialog):
completeSig = pyqtSignal()
def __init__(self, parent=None):
super(FileLoader, self).__init__(parent)
self.filename = ""
self.clientcode = ""
self.thread = ""
loadUi("GlobalUI/fileloader.ui", self)
self.prgLoader.setValue(0)
@pyqtSlot()
def on_btnCancel_clicked(self):
self.close()
def closeEvent(self, e):
self.thread.stop()
def loadData(self):
self.thread.totalSig.connect(self.prgLoader.setMaximum)
self.thread.countSig.connect(self.prgLoader.setValue)
self.thread.finished.connect(self.completed)
self.thread.start()
def completed(self):
self.completeSig.emit()
self.close()
loader.py
from PyQt5.QtCore import pyqtSignal, QThread
from fileloader import FileLoader
class DataLoader(QThread):
totalSig = pyqtSignal(int)
countSig = pyqtSignal(int)
def __init__(self, parent=None):
super(DataLoader, self).__init__(parent)
self.threadactive = True
self.commitsize = 300
self.rowdata = []
def run(self):
print("Code Will Go Here For Loading the File")
def stop(self):
self.threadactive = False
self.wait()
class PatDataLoader():
def load(self, clientcode, filename):
fl = FileLoader()
fl.clientcode = clientcode
fl.filename = filename
fl.thread = DataLoader()
fl.loadData()
我正在从另一个模块调用PatDataLoader.load("test","test.txt")。我遇到的问题是应用程序因QThread: Destroyed while thread is still running 而崩溃,因为我传递给文件加载器的线程进程似乎存在问题。我没有把这些部分正确地放在一起吗?
main.py:
from lmdb.patloader import PatDataLoader
class PPSReportsApp(QMainWindow):
def __init__(self, *args):
super(PPSReportsApp, self).__init__(*args)
loadUi("GlobalUI/ppsreportswindow.ui", self)
#self.showMaximized()
@pyqtSlot()
def on_actionTest_triggered(self):
pl = PatDataLoader()
pl.load("TEST","testfile.txt")
【问题讨论】:
-
你可以展示你如何调用PatDataLoader,也就是main。
-
我从 QMainWindow 类的菜单触发器中调用它。查看编辑的代码
-
错字:
self.thread **a** ctive = True和self.thread **A** ctive = False -
啊,是的。我纠正了错字;但是,问题仍然存在。它永远不会到达线程的 run 方法。
-
是的,我知道,但是拼写错误会分散注意力,你应该避免不要用它们分散我们的注意力。
标签: python python-3.x pyqt pyqt5 qdialog