栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Ui_MainWindow中更新pyqt5进度栏

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Ui_MainWindow中更新pyqt5进度栏

不建议修改设计文件,建议创建另一个文件以将逻辑与设计结合在一起。因此,我将假定设计文件名为ui_mainwindow.py(您必须删除所有更改)

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

您的代码有些混乱,因此我可以自由地加以改进,在这种情况下,我将不使用QThread,而是使用带有QRunnable的QThreadPool,并使用QmetaObject发送信息:

QThreadPool
QRunnable

sendInvoice.py

from PyQt5 import QtCoreimport requestsimport jsonclass 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))

我们将在将创建小部件的main.py中加入这两个部分,并建立连接:

main.py

from PyQt5 import QtCore, QtGui, QtWidgetsfrom ui_mainwindow import Ui_MainWindowfrom sendInvoice import InvoiceRunnableclass 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_())

QThread

sendInvoice.py

from PyQt5 import QtCoreimport requestsimport jsonclass 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, QtWidgetsfrom ui_mainwindow import Ui_MainWindowfrom sendInvoice import InvoiceThreadclass 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_())


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号