Python Forum
How To Make A PyQt5 Progress Bar Run While Executing A Function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: How To Make A PyQt5 Progress Bar Run While Executing A Function (/thread-42032.html)



How To Make A PyQt5 Progress Bar Run While Executing A Function - rcwildabeast - Apr-25-2024

I am relatively new to PYQT5. I am implementing a progress bar. However, I have run into the issue where the pop up progress bar does not run at the same time as when the function is called. In the example, I am using I want the progress bar to pop up and display progress while the self.main_window_button is clicked and executes a function under another file called untitled3.



import sys
import time
import untitled3
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets,QtGui
from PyQt5.QtWidgets import QDialog, QApplication,QMainWindow, QWidget, QHBoxLayout, QProgressBar,QVBoxLayout
from PyQt5.QtCore import QThread, pyqtSignal, QObject, pyqtSlot

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow,self).__init__()
        loadUi("app_interface.ui",self)
        self.pushButton.clicked.connect(self.gotoScreen2)
        
    def gotoScreen2(self):
        screen2=Screen2()
        widget.addWidget(screen2)
        widget.setCurrentWidget(screen2)
        

        
class Screen2(QDialog):
    def __init__(self):
        super(Screen2,self).__init__()
        loadUi("GL_Dump_Curr.ui",self)
        self.h_box = QHBoxLayout(self)
        self.popup = PopUpProgressB()
        self.main_window_button=self.pushButton
        self.main_window_button.clicked.connect(self.popup.start_progress)
        self.h_box.addWidget(self.main_window_button)
        self.setLayout(self.h_box)
        self.show()
        self.pushButton.clicked.connect(untitled3.gl_dump)
        #self.h_box.processEvents()
        
        self.pushButton_2.clicked.connect(self.gotoScreen1)
        
    
    def gotoScreen1(self):
        mainwindow=MainWindow()
        widget.addWidget(mainwindow)
        widget.setCurrentWidget(mainwindow)
        
    
class Worker(QObject):
    finished = pyqtSignal()
    intReady = pyqtSignal(int)

    @pyqtSlot()
    def proc_counter(self):  # A slot takes no params
        for i in range(1, 100):
            time.sleep(0.01)
            self.intReady.emit(i)
            

        self.finished.emit()


class PopUpProgressB(QWidget):

    def __init__(self):
        super().__init__()
        self.pbar = QProgressBar(self)
        self.pbar.setGeometry(30, 40, 500, 75)
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.pbar)
        self.setLayout(self.layout)
        self.setGeometry(300, 300, 550, 100)
        self.setWindowTitle('Progress Bar')
        # self.show()

        self.obj = Worker()
        self.thread = QThread()
        self.obj.intReady.connect(self.on_count_changed)
        self.obj.moveToThread(self.thread)
        self.obj.finished.connect(self.thread.quit)
        self.obj.finished.connect(self.hide)  # To hide the progress bar after the progress is completed
        self.thread.started.connect(self.obj.proc_counter)
        # self.thread.start()  # This was moved to start_progress

    def start_progress(self):
        # To restart the progress every time
        self.show()
        self.thread.start()

    def on_count_changed(self, value):
        self.pbar.setValue(value)
    
   
    
        
        
app = QApplication(sys.argv)
widget=QtWidgets.QStackedWidget()
mainwindow=MainWindow()
widget.addWidget(mainwindow)
widget.setFixedHeight(300)
widget.setFixedHeight(400)
widget.show()

try:
    sys.exit(app.exec_())
except:
    print("Exiting")



RE: How To Make A PyQt5 Progress Bar Run While Executing A Function - menator01 - Apr-26-2024

Maybe this link will help. Seems to be what you are trying to do.
https://realpython.com/python-pyqt-qthread/