I'm completely new to PyQt5 and I'm having trouble to connect a function to a QtProgressBar.
I'm using the script shown in this answer to create the popup, thread, etc. There, they connect the progress bar with the Worker Class. How can I connect my function 'testFunction' to make the progress bar run while this function is running?
import sys
import pandas as pd
import time
from PyQt5.QtCore import QThread, pyqtSignal, QObject, pyqtSlot
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout, QProgressBar, QVBoxLayout
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Widget")
self.h_box = QHBoxLayout(self)
self.main_window_button = QPushButton("Start")
self.popup = PopUpProgressB() # Creating an instance instead as an attribute instead of creating one
# everytime the button is pressed
self.main_window_button.clicked.connect(self.popup.start_progress) # To (re)start the progress
self.h_box.addWidget(self.main_window_button)
self.setLayout(self.h_box)
self.show()
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.1)
self.intReady.emit(i)
self.finished.emit()
def testFunction(self, rvg, mes):
df = pd.read_excel(self.rvg, sheet_name=self.mes,
skiprows=2, decimal=',')
df = df.dropna(how='all')
df_obj = df.select_dtypes(['object'])
df[df_obj.columns] = df_obj.apply(lambda x: x.str.strip())
df['RFO'] = df['RFO'].str.strip().replace(' ', ' ')
df = df.T
df = df.groupby(level=0).sum()
df.columns = df.loc['NOME']
df = df.drop(index='NOME')
df.to_excel('testFunction.xlsx', encoding='utf-8',
sheet_name='testFunction')
return df
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.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)
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)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())