重置QProgressBar以再次运行

时间:2018-07-21 13:32:18

标签: python-3.x pyqt5 qthread qtimer qprogressbar

我有一个用于下载数据的ProgressBar。

有时候任务可能会超时,如果可以,我希望能够重新启动该过程。

  1. 任务已启动
  2. 计时器已启动
  3. 如果计时器结束,请结束任务
  4. 按下按钮以重新启动任务

除了progressbar不会重新开始以外,以上所有方法都可以正常工作。

我可以将其重置为0,但它不会移动(在它被卡在100%之前!)

如果任务在计时器用尽之前完成,进度条将再次正常工作。

如果计时器在任务之前用完了,如何重新启动进度栏?

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QProgressBar, QPushButton
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class DownloadThread(QThread):
    taskFinished = pyqtSignal()

    def __init__(self, parent=None):
        super(DownloadThread, self).__init__(parent)

    def run(self):
        for i in range(20000):
            print(i)
        self.taskFinished.emit()


class Example(QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      
        # Set Progress Bard
        self.progressBar = QProgressBar(self)
        self.progressBar.setGeometry(30, 40, 200, 25)

        # Button
        self.btn = QPushButton('Start', self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.start_download_thread)

        # Set Timer
        self.timer = QBasicTimer()
        self.step = 0

        # Display        
        self.setGeometry(300, 300, 280, 170)
        self.show()

    def start_download_thread(self):
        # Set Progress bar to 0%
        self.progressBar.setValue(0)


        #Start Thread
        self.Download = DownloadThread()
        self.Download.taskFinished.connect(self.onDownloaded)
        self.onDownloading()
        self.Download.start()    

    def onDownloading(self):
        #start the timer
        self.progressBar.show()
        self.timerSwitch()


    def timerSwitch(self):
        # Turn timer off or on
        if self.timer.isActive():
            self.timer.stop()
        else:
            self.timer.start(2, self)

    def onDownloaded(self):
        # Stop the timer
        self.timerSwitch()
    # progress bar 100%
        self.progressBar.setValue(1)
        self.progressBar.setRange(0, 1)

    def timerEvent(self, e): 
        if self.step >= 100:
            self.timer.stop()
            # self.Download.quit()

            return
        self.step = self.step + 1
        self.progressBar.setValue(self.step)


def main():

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

我通过将Qtimer()步骤移到start_download_thread

来解决了这个问题
def start_download_thread(self):      

    self.step = 0 # reset the steps to 0

这意味着它会重置步骤,进度栏会再次开始。

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QProgressBar, QPushButton
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class DownloadThread(QThread):
    taskFinished = pyqtSignal()

    def __init__(self, parent=None):
        super(DownloadThread, self).__init__(parent)

    def run(self):
        for i in range(20000):
            print(i)
        self.taskFinished.emit()


class Example(QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      
        # Set Progress Bard
        self.progressBar = QProgressBar(self)
        self.progressBar.setGeometry(30, 40, 200, 25)

        # Button
        self.btn = QPushButton('Start', self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.start_download_thread)

        # Set Timer
        self.timer = QBasicTimer()

        # Display        
        self.setGeometry(300, 300, 280, 170)
        self.show()

    def start_download_thread(self):       

        self.step = 0

        # Set Progress bar to 0%
        self.progressBar.setValue(0)       

        #Start Thread
        self.Download = DownloadThread()
        self.Download.taskFinished.connect(self.onDownloaded)
        self.onDownloading()
        self.Download.start()

    def onDownloading(self):
        #start the timer
        self.progressBar.show()
        self.timerSwitch()

    def timerSwitch(self):
        # Turn timer off or on
        if self.timer.isActive():
            self.timer.stop()
        else:
            self.timer.start(2, self)

    def onDownloaded(self):
        # Stop the timer
        self.timerSwitch()
    # progress bar 100%
        self.progressBar.setValue(100)
        self.progressBar.setRange(0, 100)

    def timerEvent(self, e): 
        if self.step >= 100:
            self.timer.stop()
            return
        self.step = self.step + 1
        self.progressBar.setValue(self.step)


def main():

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()