How do I update pyqt5 progress bar in Ui_MainWindow
Asked Answered
U

1

1

I try to make the progress bar update real-time with pyqt5, however, the documentation online seems very limited for now. I read online that I can use thread and signal to do that. However, the syntax seems changed in pyqt5. Can any experts here help?

Main ui window.

# -*- coding: utf-8 -*-
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from sendInvoice import *

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(441, 175)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
        self.progressBar.setGeometry(QtCore.QRect(30, 20, 411, 61))
        self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName("progressBar")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(160, 100, 75, 23))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 441, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.pushButton.clicked.connect(self.sendInvoice)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Send"))

    def sendInvoice(self):
        sendInvoice.sendInv()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

worker file where the invoice is sending out.

import requests
import json
import sys

from PyQt5.QtWidgets import (QWidget, QProgressBar, QPushButton, QApplication)





class sendInvoice():

    def sendInv(self):
        startInvNum = int(100)
        endInvNum = int(102)
        Username = 'test'
        Password = 'test'
        AccountNum = test
        environmentURL = 'http://www.test.com/api?INV' ##remove this temporary
        totalRequest = int(endInvNum) - int(startInvNum)


        n = 1

        headerData = {
                     'Authorization': 'auth_email=' + Username + ', auth_signature=' + Password + ', auth_account=' + AccountNum,
                     'content-type': 'application/json',
        }

        QApplication.processEvents()


        for i in range(startInvNum, endInvNum+1):
            result = requests.get(environmentURL + str(i), headers=headerData)

            print (result.text)
            jsonOutput = json.loads(result.text)
            print (json.dumps(jsonOutput, sort_keys=True, indent=4))
            self.currentCount = str(n)
            self.total = str(totalRequest)
            percentage = sendInvoice.getPercentage(self)
            print (percentage)
            QApplication.processEvents()
            n += 1

    def getPercentage(self):

        count = int(self.currentCount)
        total = int(self.total)
        currentPercentage = (count / (total + 1) * 100)
        print(currentPercentage * 100)
        return currentPercentage
Until answered 30/3, 2018 at 23:16 Comment(8)
sendInvoice works ?, I just tried it and it generates an errorBinnings
sendInvoice connects to a server-side restlet script. It will not work for standalone. The script in the backend are all fine. It is just the GUI. I want to make the progress bar moving while sending out invoice. Giving the client % of finish.Until
Yes it works alone but I get an error because you do not get a json and so json.loads gives an error.Binnings
explain yourself better I do not understand.Binnings
The script calls the restlet API on the server-side to send out invoice. I only need to provide the header for authentication, and everything is passthrough the URL. test.com/….Until
basically I think you can remove the json portion. I just want to show myself the response from the server. print (result.text) jsonOutput = json.loads(result.text) print (json.dumps(jsonOutput, sort_keys=True, indent=4))Until
ok, I understand, in a moment publish an answer.Binnings
thx u so much, and sorry for the confusion.Until
B
3

It is not recommended to modify the design file, it is appropriate to create another file to join the logic with the design. Therefore, I will assume that the design file is called ui_mainwindow.py( You must delete all changes)

.
├── main.py
├── sendInvoice.py
└── ui_mainwindow.py

Your code is a little messy so I take the liberty to improve it, in this case I will not use QThread, but QThreadPool with a QRunnable, and to send the information I will use the QMetaObject:

with QThreadPool and QRunnable

sendInvoice.py

from PyQt5 import QtCore

import requests
import json

class InvoiceRunnable(QtCore.QRunnable):
    def __init__(self, progressbar):
        QtCore.QRunnable.__init__(self)
        self.progressbar = progressbar

    def run(self):
        startInvNum = 100
        endInvNum = 102
        Username = 'test'
        Password = 'test'
        AccountNum = 'test'
        environmentURL = 'http://www.test.com/api?INV' ##remove this temporary


        headerData = { 
            'Authorization': 'auth_email={}, auth_signature={}, auth_account={}'.format(Username, Password, AccountNum),
            'content-type': 'application/json',
        }

        totalRequest = endInvNum - startInvNum + 1 

        for n, i in enumerate(range(startInvNum, endInvNum+1)):
            result = requests.get(environmentURL + str(i), headers=headerData)

            print (result.text)
            jsonOutput = json.loads(result.text)
            print(json.dumps(jsonOutput, sort_keys=True, indent=4))
            print(n+1, totalRequest)
            currentPercentage = (n+1)*100/totalRequest
            QtCore.QMetaObject.invokeMethod(self.progressbar, "setValue",
                                 QtCore.Qt.QueuedConnection,
                                 QtCore.Q_ARG(int, currentPercentage))

we will join both parts in the main.py where the widget will be created and make the connection:

main.py

from PyQt5 import QtCore, QtGui, QtWidgets
from ui_mainwindow import Ui_MainWindow
from sendInvoice import InvoiceRunnable

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.progressBar.setRange(0, 100)
        self.pushButton.clicked.connect(self.sendInvoice)

    def sendInvoice(self):
        runnable = InvoiceRunnable(self.progressBar)
        QtCore.QThreadPool.globalInstance().start(runnable)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

with QThread

sendInvoice.py

from PyQt5 import QtCore

import requests
import json

class InvoiceThread(QtCore.QThread):
    percentageChanged = QtCore.pyqtSignal(int)

    def run(self):
        startInvNum = 100
        endInvNum = 102
        Username = 'test'
        Password = 'test'
        AccountNum = 'test'
        environmentURL = 'http://www.test.com/api?INV' ##remove this temporary


        headerData = { 
            'Authorization': 'auth_email={}, auth_signature={}, auth_account={}'.format(Username, Password, AccountNum),
            'content-type': 'application/json',
        }

        totalRequest = endInvNum - startInvNum + 1 

        for n, i in enumerate(range(startInvNum, endInvNum+1)):
            result = requests.get(environmentURL + str(i), headers=headerData)

            print (result.text)
            jsonOutput = json.loads(result.text)
            print(json.dumps(jsonOutput, sort_keys=True, indent=4))
            print(n+1, totalRequest)
            currentPercentage = (n+1)*100/totalRequest
            self.percentageChanged.emit(currentPercentage)

main.py

from PyQt5 import QtCore, QtGui, QtWidgets
from ui_mainwindow import Ui_MainWindow
from sendInvoice import InvoiceThread

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.progressBar.setRange(0, 100)
        self.pushButton.clicked.connect(self.sendInvoice)

    def sendInvoice(self):
        thread = InvoiceThread(self)
        thread.percentageChanged.connect(self.progressBar.setValue)
        thread.start()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
Binnings answered 31/3, 2018 at 1:33 Comment(1)
@ReeceMak I did not say that they are not appropriate, but it is easier to see it with QthreadPool with QRunnable since it is of a higher level, you could also use QThread but it is of a lower level.Binnings

© 2022 - 2024 — McMap. All rights reserved.