Appreciate if you could help me. I have a trouble showing up the progress bar while saving file to excel. What I want to achieve is to show a progress bar while saving excel file from a pandas dataframe
also from qwidgettable
as it takes time before it saves. Until the excel file is downloaded or saved i want that progress bar to close. I tried looking over the net but I cant see specific answers to my query. So far, this is the compiled codes I have created.
import sys
from PyQt5 import QtWidgets, QtCore
import pandas as pd
import time
import psutil
class ThreadClass(QtCore.QThread):
updateProgressBar = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(ThreadClass, self).__init__(parent)
def run(self):
while True:
val = int(psutil.cpu_percent())
time.sleep(1)
self.updateProgressBar.emit(val)
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50,50,500,500)
self.setWindowTitle('PyQt Tuts')
self.table()
def updateProgressBar(self, val):
self.progressBar.setValue(val)
def table(self):
self.tableWidget = QtWidgets.QTableWidget()
self.tableWidget.setGeometry(QtCore.QRect(220, 100, 411, 392))
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(5)
self.tableWidget.show()
item = QtWidgets.QTableWidgetItem()
item.setText("Amount")
self.tableWidget.setHorizontalHeaderItem(1, item)
records = [
['Product 1', 1000],
['Product 2', 500],
['Product 3', 600],
['Product 4', 300],
['Product 5', 800],
]
self.df = pd.DataFrame(records, columns=['Name', 'Amount'])
for r in range(5):
for c in range(2):
table_item = str(self.df.iloc[r, c])
self.tableWidget.setItem(r, c, QtWidgets.QTableWidgetItem(table_item))
self.pb_extract = QtWidgets.QPushButton(self.tableWidget)
self.pb_extract.setGeometry(QtCore.QRect(10, 200, 75, 23))
self.pb_extract.clicked.connect(self.extract)
self.pb_extract.setText("EXTRACT")
self.pb_extract.show()
def extract(self):
self.lb_downloading = QtWidgets.QLabel(self.tableWidget)
self.lb_downloading.setGeometry(QtCore.QRect(10, 270, 81, 16))
self.lb_downloading.setText("Downloading..")
self.lb_downloading.show()
self.progressBar = QtWidgets.QProgressBar(self.tableWidget)
self.progressBar.setGeometry(QtCore.QRect(10, 290, 171, 10))
self.progressBar.show()
self.threadclass = ThreadClass()
self.threadclass.start()
self.threadclass.updateProgressBar.connect(self.updateProgressBar)
self.df.to_excel('Products.xlsx', index=False)
print('Download complete!')
def run():
app = QtWidgets.QApplication(sys.argv)
app.setStyle("fusion")
w = Window()
sys.exit(app.exec_())
run()
these codes look like this:
what i want to achieve is when i click the extract button, the downloading progressbar will close until the excel file fully downloaded/saved.
(P.S i just get random values for val = int(psutil.cpu_percent())
because i also don't know what specific code/function to use while the app is running just to show to you that i have a progress bar moving.)
Thank you in advance!