我正在尝试使用pyqt5使工作成为一个进度条但是当我打开进度条的窗口时,我得到一个空白的窗口。
从ui生成的进度条的代码是:
从ui生成的Ui_dlg_progress:
从PyQt5导入QtCore,QtGui,QtWidgets
class Ui_dlg_progress(object):
def setupUi(self, dlg_progress):
dlg_progress.setObjectName("dlg_progress")
dlg_progress.resize(376, 91)
dlg_progress.setSizeGripEnabled(True)
self.gridLayout = QtWidgets.QGridLayout(dlg_progress)
self.gridLayout.setObjectName("gridLayout")
self.label = QtWidgets.QLabel(dlg_progress)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_num_folders = QtWidgets.QLabel(dlg_progress)
self.label_num_folders.setObjectName("label_num_folders")
self.gridLayout.addWidget(self.label_num_folders, 0, 1, 1, 1)
self.label_text = QtWidgets.QLabel(dlg_progress)
self.label_text.setObjectName("label_text")
self.gridLayout.addWidget(self.label_text, 0, 2, 1, 1)
self.label_total_folders = QtWidgets.QLabel(dlg_progress)
self.label_total_folders.setObjectName("label_total_folders")
self.gridLayout.addWidget(self.label_total_folders, 0, 3, 1, 1)
self.progressBar = QtWidgets.QProgressBar(dlg_progress)
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.gridLayout.addWidget(self.progressBar, 1, 0, 1, 4)
self.retranslateUi(dlg_progress)
QtCore.QMetaObject.connectSlotsByName(dlg_progress)
def retranslateUi(self, dlg_progress):
_translate = QtCore.QCoreApplication.translate
dlg_progress.setWindowTitle(_translate("dlg_progress", "Processing"))
self.label.setText(_translate("dlg_progress", "Processing:"))
self.label_num_folders.setText(_translate("dlg_progress", "0"))
self.label_text.setText(_translate("dlg_progress", "folders of a total of: "))
self.label_total_folders.setText(_translate("dlg_progress", "1"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
dlg_progress = QtWidgets.QDialog()
ui = Ui_dlg_progress()
ui.setupUi(dlg_progress)
dlg_progress.show()
ProgressBar.py - 调用Ui_dlg_progress
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog
from Ui_progress_bar import Ui_dlg_progress
class dlg_progress(QDialog, Ui_dlg_progress):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor
@param parent reference to the parent widget
@type QWidget
"""
super(dlg_progress, self).__init__(parent)
self.setupUi(self)
self.show()
主要代码在哪里称为进度条:
def main():
subdirs = [f for f in source.iterdir() if f.is_dir()]
if len(subdirs) == 0:
return -1
prog = dlg_progress()
#Actual number of folders procecessed
proc_folders = 0
prog.progressBar.setValue(proc_folders)
prog.label_num_folders.setText(str(proc_folders))
prog.label_total_folders.setText(str(len(subdirs)))
prog.progressBar.maximum = len(subdirs)
for subdir in subdirs:
process of the folders
#Number of processed folders
proc_folders += 1
prog.setValue(proc_folders)
prog.label_num_folders.setText(str(proc_folders))
QApplication.processEvents()
return 0
答案 0 :(得分:1)
在最后一个循环中看起来像是一个拼写错误:
html_attributes
否则,您应该测试for subdir in subdirs:
#Number of processed folders
proc_folders += 1
prog.progressBar.setValue(proc_folders)
prog.label_num_folders.setText(str(proc_folders))
QApplication.processEvents()
和生成列表的长度,如果它为空,则循环不会启动,进度条将保持为0。