我已经玩了大约三个月的python,几天前我需要做一些琐碎的工作(创建文件夹),实际上我做了一个脚本。你知道吗
出于纯粹的完美主义,我想添加一个进度条,但当我运行脚本时,它挂起,然后说一旦完成。我想看到进度条去100%,而不是挂起。我已经读到我需要使用python线程来实现这一点,但我真的很困惑如何实现它,但也理解它。你知道吗
我复制了理论上需要执行的代码部分。如您所见,我使用的是QT设计器UI和进度条。你知道吗
间距也不是问题。这是我第一次发布堆栈溢出,所以请原谅我,如果我弄乱了这里的代码间距。你知道吗
非常感谢你能为我解释这件事。你知道吗
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)
通常最好将业务逻辑从GUI代码中移出,并将其放在不依赖Qt的单独模块中。这使得它更容易在另一个线程中执行它。你知道吗
对于需要进度反馈的函数,我通常将它们转换为生成器。你知道吗
您可以在另一个线程中创建一个worker对象,该线程将执行此业务逻辑并使用信号将进度报告回主GUI线程,以便进度条可以更新
然后将这个worker添加到GUI类并连接到signals
相关问题 更多 >
编程相关推荐