Python Forum
How To Make A PyQt5 Progress Bar Run While Executing A Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How To Make A PyQt5 Progress Bar Run While Executing A Function
#1
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")
Reply
#2
Maybe this link will help. Seems to be what you are trying to do.
https://realpython.com/python-pyqt-qthread/
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help needed running a simple function in pyqt5 diodes 27 8,670 Jan-24-2023, 12:19 PM
Last Post: GetOnData
  [PyQt] Generate Progress Bar while executing a task maiya 7 8,267 Aug-10-2022, 10:05 AM
Last Post: mfitzp
  Stop import from executing function, but allow the function to execute from tk btn. MrBitPythoner 4 2,763 Dec-08-2020, 10:00 PM
Last Post: MrBitPythoner
  PyQt5 How do you make a functioning login and registration program? YoshikageKira 4 7,258 Jan-17-2020, 09:51 PM
Last Post: pavulon
  [PyQt] Pyqt5: How do you make a button that adds new row with data to a Qtablewidget YoshikageKira 6 7,112 Jan-02-2020, 04:32 PM
Last Post: Denni
  How can I measure progress and display it in the progress bar in the interface? Matgaret 2 5,986 Dec-11-2019, 03:30 PM
Last Post: Denni
  [Tkinter] Progress Bar While Sending an Email maxtimbo 3 4,157 Oct-09-2019, 09:13 PM
Last Post: woooee
  Progress Bar While Sending an Email maxtimbo 0 2,144 Oct-08-2019, 02:13 PM
Last Post: maxtimbo
  [Tkinter] How make a button perform a function after the user inputs numbers Zephyrforce 1 2,475 May-22-2019, 05:43 PM
Last Post: woooee
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,861 Apr-06-2019, 11:15 PM
Last Post: ZenWoR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020