不建议修改设计文件,建议创建另一个文件以将逻辑与设计结合在一起。因此,我将假定设计文件名为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_())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)