pyqt4中时间未知过程中的进度条

时间:2018-06-20 06:45:28

标签: python python-3.x pyqt4 qprogressbar

我是pyQt4的新手,我需要使用pyQt4和python 3.5创建GUI。 我的流程很长,没有确切的执行时间。时间持续时间根据我每次在流程中使用的输入而有所不同。有一些关于使用timers.pro添加进度条的示例,但是在我的情况下,我不知道确切的持续时间。

我需要根据该过程花费的时间来完成进度条。如在我的代码中,此过程是methodtobeexecute()。有人可以帮我解决带有代码示例的解释此问题。预先感谢...。

这是我的代码,到目前为止它已经使用QTimer完成了进度条。但是我想要的是完成了进度条,同时methodtobeexecute()方法完成了它的任务。(QTimer仅用于检查此处的进度条)< / p>

    # -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'pro2.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
import sys

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.progressBar = QtGui.QProgressBar(Form)
        self.progressBar.setGeometry(QtCore.QRect(160, 60, 118, 23))
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName(_fromUtf8("progressBar"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(170, 140, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        # # restarts the 'QProgressBar'
        self.progressBar.reset()

        # creates a 'QTimer'
        self.qtimer = QtCore.QTimer()

        # connects the 'QTimer.timeout()' signal with the 'qtimer_timeout()' slot
        self.qtimer.timeout.connect(self.qtimer_timeout)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "start", None))
        self.pushButton.clicked.connect(self.qpushbutton_clicked)

    def qpushbutton_clicked(self):

        self.progressBar.reset()

        #this is the method that needs the time to complete the progress bar
        self.methodtobeexecute()

        self.qtimer.start(40)

    def qprogressbar_value_changed(self, value):
        if value == self.progressBar.maximum():
            # stops the 'QTimer'
            self.qtimer.stop()

    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)

     def methodtobeexecute(self):
         for i in 400:
             print(i)





def main():
    app = QtGui.QApplication(sys.argv)
    mw = Ui_Form()
    mw.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

0 个答案:

没有答案