add a progress bar before opening a window
-
I have a pyqt project where there is a large file processing, when I click on a button (Browse Folder) that will open a window containing treeviews that present the hierarchy of files, the window remains white for a few seconds, so here, I want to display a progress bar (like the loading one), is it possible to do it? if not, please suggest me any other idea.
Here is my code :class StartWindow(QDialog): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle('Open ODL File or Folder') self.setWindowIcon(QIcon('assets/acls_icon.png')) self.setWindowFlags(self.windowFlags() | Qt.WindowMinimizeButtonHint) self.resize(600, 400) main_layout = QHBoxLayout(self) buttons_layout = QVBoxLayout() self.choose_file_button = QPushButton('Browse File', self) self.choose_file_button.clicked.connect(self.choose_file) buttons_layout.addWidget(self.choose_file_button) self.choose_folder_button = QPushButton('Browse Folder', self) self.choose_folder_button.clicked.connect(self.choose_folder) buttons_layout.addWidget(self.choose_folder_button) main_layout.addLayout(buttons_layout) def choose_folder(self): folder_path = QFileDialog.getExistingDirectory(self, "Open Folder Containing ODL Files") if folder_path: self.open_folder_view(folder_path) def open_folder_view(self, folder_path): folder_window = QDialog() folder_window.setWindowFlags(self.windowFlags() | Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint) folder_window.setWindowTitle('ODL Files Tree View') folder_window.resize(800, 600) folder_window.show() processed_files = set() all_groups = set() self.folder_path = folder_path for root, dirs, files in os.walk(folder_path): for filename in files: if (filename.endswith(".odl")) and ('acl' in filename.lower() or 'acls' in filename.lower()) and filename not in processed_files: file_path = os.path.join(root, filename) parser = ODLParser(file_path) file_groups, _ = get_headers_and_rights(parser.data_model) all_groups.update(file_groups) tree_view = self.create_tree_view(parser) scroll_layout.addWidget(tree_view) processed_files.add(filename) elif (filename.endswith(".odl")) and not ('acl' in filename.lower() or 'acls' in filename.lower()) and filename not in processed_files: file_path = os.path.join(root, filename) with open(file_path, 'r', encoding='utf-8') as file: content = file.read() if "acl" in content.lower() and "{" in content.lower(): parser = ODLParser2(file_path) file_groups, _ = get_headers_and_rights(parser.data_model) all_groups.update(file_groups) tree_view = self.create_tree_view(parser) scroll_layout.addWidget(tree_view) processed_files.add(filename) valid_groups = [group for group in all_groups if group is not None] self.group_combobox.addItems(sorted(valid_groups)) self.group_combobox.setCurrentIndex(0) self.group_combobox.currentTextChanged.connect(self.on_group_combobox_changed) QTimer.singleShot(0, lambda: self.on_group_combobox_changed(self.group_combobox.currentText())) self.scroll_area.setWidget(scroll_widget) layout.addWidget(self.scroll_area) folder_window.exec_()
-
@sof4Rou
See https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QProgressBar.html and https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QProgressDialog.html.You could also probably avoid the "white window"/non-responsive GUI if you split your
for root, dirs, files in os.walk(folder_path)
and/orfor filename in files
loop(s) so that it did one/a few elements at a time on aQTimer
.