循环时如何更新进度条?

时间:2019-12-18 20:08:39

标签: python pyqt5

我创建了帖子How do I update a progress bar while looping? (Both in different windows),并查看了相关文章,但不幸的是,给出的解决方案并没有太大帮助,特别是因为它们在此问题上对我有帮助:

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”窗口,随着循环实时进行,进度会更新,我该怎么做?

1 个答案:

答案 0 :(得分:0)

请勿更改在Qt Designer中创建的模块。

while循环会冻结接口。您需要使用threading + signalsQTimer

尝试一下:

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, 120)
        self.buttonBox = QtWidgets.QDialogButtonBox(win_2)
        self.buttonBox.setGeometry(QtCore.QRect(25, 85, 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"))


class Win_2(QtWidgets.QDialog, Ui_win_2):
    def __init__(self):
        super(Win_2, self).__init__()
        self.setupUi(self)

        self.progressBar.setMinimumWidth(360)
        self.progressBar.setMinimumHeight(55)
        self.progressBar.reset()
        self.progressBar.setMaximum(112)
        self.progressBar.setAlignment(QtCore.Qt.AlignCenter)
        self.progressBar.valueChanged.connect(self.qprogressbar_value_changed)
        self.progressBar.setFormat('percentage completed: %p%, current value: %v, maximum value: %m')   

        # creates a 'QTimer'
        self.qtimer = QtCore.QTimer()
        self.qtimer.timeout.connect(self.qtimer_timeout)  
        # starts QTimer at intervals of 50 milliseconds
        self.qtimer.start(50)        

    @QtCore.pyqtSlot(int)
    def qprogressbar_value_changed(self, value):
        # if the QProgressBar reaches its maximum value
        if value == self.progressBar.maximum():
            # stops the 'QTimer'
            self.qtimer.stop()     

    @QtCore.pyqtSlot()
    def qtimer_timeout(self):
        # gets the current value of the 'QProgressBar'
        value = self.progressBar.value()
        # adds 1 to the current value of the 'QProgressBar'
        self.progressBar.setValue(value + 1)


class Win_1(QtWidgets.QDialog, Ui_win_1):
    def __init__(self):
        super(Win_1, self).__init__()
        self.setupUi(self)   

    def exec_win_2(self):
        self.win_2 = Win_2()
        self.win_2.show()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)

    w = Win_1()
    w.show()

enter image description here