重置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()

我通过将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()

相关内容

  • 没有找到相关文章

最新更新