循环时如何更新进度条? (都在不同的窗口中)

时间:2019-12-18 16:49:30

标签: python pyqt pyqt5

我是社区的新手,并且开始编程。我的问题基本上就是标题中描述的问题,下面的代码如下:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_win_1(object):

    def setupUi(self, win_1):
        win_1.setObjectName("win_1")
        win_1.resize(500, 100)
        self.pushButton = QtWidgets.QPushButton(win_1)
        self.pushButton.setGeometry(QtCore.QRect(210, 40, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.retranslateUi(win_1)
        QtCore.QMetaObject.connectSlotsByName(win_1)

        self.pushButton.clicked.connect(self.exec_win_2)

    def retranslateUi(self, win_1):
        _translate = QtCore.QCoreApplication.translate
        win_1.setWindowTitle(_translate("win_1", "Win_1"))
        self.pushButton.setText(_translate("win_1", "Run"))

    def exec_win_2(self):
        num = 0
        per = 0
        win_2.show()
        while num < 10000000:
            num += 1
            if (num/100000).is_integer():
                per += 1
                ui2.progressBar.setValue(per)
        win_2.close()


class Ui_win_2(object):

    def setupUi(self, win_2):

        win_2.setObjectName("win_2")
        win_2.resize(400, 100)
        self.buttonBox = QtWidgets.QDialogButtonBox(win_2)
        self.buttonBox.setGeometry(QtCore.QRect(20, 60, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.progressBar = QtWidgets.QProgressBar(win_2)
        self.progressBar.setGeometry(QtCore.QRect(20, 20, 361, 23))
        self.progressBar.setObjectName("progressBar")
        self.retranslateUi(win_2)
        self.buttonBox.accepted.connect(win_2.accept)
        self.buttonBox.rejected.connect(win_2.reject)
        QtCore.QMetaObject.connectSlotsByName(win_2)

    def retranslateUi(self, win_2):

        _translate = QtCore.QCoreApplication.translate
        win_2.setWindowTitle(_translate("win_2", "Win_2"))

if __name__ == "__main__":

    import sys
    app = QtWidgets.QApplication(sys.argv)
    win_1 = QtWidgets.QDialog()
    win_2 = QtWidgets.QDialog()
    ui = Ui_win_1()
    ui.setupUi(win_1)
    ui2 = Ui_win_2()
    ui2.setupUi(win_2)
    win_1.show()
    sys.exit(app.exec_())

重点在于:

def exec_win_2(self):
        num = 0
        per = 0
        win_2.show()
        while num < 10000000:
            num += 1
            if (num/100000).is_integer():
                per += 1
                ui2.progressBar.setValue(per)
        win_2.close()

当您单击“运行”按钮时,我想打开“ win_2”窗口,随着循环实时进行,进度会更新,我该怎么做?

非常感谢您。

0 个答案:

没有答案