我一直在玩python大约三个月和几天前我需要做一些琐碎的任务(文件夹创建),我实际上做了一个脚本来做它。
出于纯粹的完美主义,我想添加一个进度条,但是当我运行脚本时,它会挂起,然后在完成后说完整。我希望看到进度条达到100%而不是悬挂。我已经读过我需要使用python线程来做到这一点,但我真的很困惑如何实现它,但也理解它。
我已经复制了理论上需要线程化的代码部分。如您所见,我正在使用QT Designer UI和Progress Bar。
间距也不是问题。这是我第一次在Stack Overflow上发帖,请原谅我,如果我搞砸了代码间距。
非常感谢你为我提供的任何光明。
class ShotCreator(QtGui.QDialog):
def __init__(self, *args):
ui = 'ShotCreator_UI.ui'
QtGui.QWidget.__init__(self, *args)
loadUi(ui, self)
self.show()
#Slots
self.connect(self.browseLocation_btn, QtCore.SIGNAL("clicked()"), self.openBrowse)
self.connect(self.create_btn, QtCore.SIGNAL("clicked()"), self.makeDir)
self.progressBar.setValue(0)
def makeDir(self):
#Get data
departments = self.queryValues()
shots = self.fullShotAmount()
proj = self.projName()
#Converting proj to string
proj = str(proj)
#Converting dirLoc to string
dirLoc = self.browseLocation.text()
dirLoc = str(dirLoc)
if dirLoc == "":
msgBox = QtGui.QMessageBox()
msgBox.setIcon(QMessageBox.Warning)
msgBox.setWindowTitle("Oops - Directory Location")
msgBox.setText("Please give a directory location")
msgBox.exec_()
#Creating shot numbers
shot = 0
for s in shots:
shot = shot + 5
sShot = ("00" + str(shot))
if not os.path.exists(sShot):
#Create shot folders
os.mkdir(sShot)
self.progressBar.setValue((int(s) / int(len(shots))* 100))
self.progressBar.setValue(100)
答案 0 :(得分:0)
通常最好将业务逻辑从GUI代码中移出,并将其放在一个不依赖于Qt的单独模块中。这使得它更容易在另一个线程中执行它。
对于我想要进度反馈的功能,我通常将它们变成生成器。
from __future__ import division
def makeDir(dirloc, shots):
cnt = len(shots)
#Creating shot numbers
shot = 0
for i, s in enumerate(shots):
shot = shot + 5
sShot = ("00" + str(shot))
if not os.path.exists(sShot):
#Create shot folders
os.mkdir(sShot)
yield int((i + 1) / cnt * 100)
您在另一个线程中创建一个工作对象,该线程将执行此业务逻辑并使用信号将进度报告回主GUI线程,以便进度条可以更新
class Worker(QObject):
progress_updated = pyqtSignal(int)
finished = pyqtSignal()
@pyqtSlot(object, object)
def makeDir(self, dirloc, shots):
for progress in makeDir(dirloc, shots):
self.progress_updated.emit(progress)
self.finished.emit()
然后将此worker添加到GUI类并连接到信号
class ShotCreator(QtGui.QDialog):
start_makedirs = pyqtSignal(object, object)
def __init__(self, *args):
ui = 'ShotCreator_UI.ui'
QtGui.QWidget.__init__(self, *args)
loadUi(ui, self)
self.show()
#Slots
self.connect(self.browseLocation_btn, QtCore.SIGNAL("clicked()"), self.openBrowse)
self.connect(self.create_btn, QtCore.SIGNAL("clicked()"), self.makeDir)
self.progressBar.setValue(0)
self.thread = QThread(self)
self.worker = Worker(self)
self.worker.progress_updated.connect(self.update_progress)
self.worker.finished.connect(self.worker_finished)
self.start_makedirs.connect(self.worker.makeDir)
self.worker.moveToThread(self.thread)
self.thread.start()
def makeDir(self):
# gather information from gui
dirloc = ''
shots = []
...
self.start_makedirs.emit(dirloc, shots)
@pyqtSlot(int)
def update_progress(self, progress):
self.progressBar.setValue(progress)
@pyqtSlot()
def worker_finished(self):
self.progressBar.setValue(100)