class CCountUp(QLabel): def __init__(self, *args, **kwargs): super(CCountUp, self).__init__(*args, **kwargs) self.isFloat = False # 是否是小数 font = self.font() or QFont() font.setBold(True) self.setFont(font) self.timeline = QTimeLine(6000, self) self.timeline.setEasingCurve(QEasingCurve.OutExpo) self.timeline.frameChanged.connect(self.onFrameChanged) def pause(self): """暂停 """ self.timeline.setPaused(True) def resume(self): """继续 """ self.timeline.resume() def isPaused(self): """是否暂停 """ return self.timeline.state() == QTimeLine.Paused def reset(self): """重置 """ self.timeline.stop() self.isFloat = False # 是否是小数 self.setText('0') def onFrameChanged(self, value): if self.isFloat: value = round(value / 100.0 + 0.00001, 2) value = str(format(value, ',')) self.setText(value + '0' if value.endswith('.0') else value) def setDuration(self, duration): """设置动画持续时间 :param duration: """ self.timeline.setDuration(duration) def setNum(self, number): """设置数字 :param number: int or float """ if isinstance(number, int): self.isFloat = False self.timeline.setFrameRange(0, number) elif isinstance(number, float): self.isFloat = True self.timeline.setFrameRange(0, number * 100) self.timeline.stop() self.setText('0') self.timeline.start()
class TimedProgressBar(QProgressBar): """A QProgressBar showing a certain time elapse.""" hideOnTimeout = True def __init__(self, parent=None): super(TimedProgressBar, self).__init__(parent, minimum=0, maximum=100) self._timeline = QTimeLine(updateInterval=100, frameChanged=self.setValue) self._timeline.setFrameRange(0, 100) self._hideTimer = QTimer(timeout=self._done, singleShot=True, interval=3000) def start(self, total, elapsed=0.0): """Starts showing progress. total is the number of seconds (maybe float) the timeline will last, elapsed (defaulting to 0) is the value to start with. """ self._hideTimer.stop() self._timeline.stop() self._timeline.setDuration(total * 1000) self._timeline.setCurrentTime(elapsed * 1000) self.setValue(self._timeline.currentFrame()) self._timeline.resume() if self.hideOnTimeout: self.show() def stop(self, showFinished=True): """Ends the progress display. If showFinished is True (the default), 100% is shown for a few seconds and then the progress is reset. The progressbar is hidden if the hideOnTimeout attribute is True. """ self._hideTimer.stop() self._timeline.stop() if showFinished: self.setValue(100) self._hideTimer.start() else: self._done() def _done(self): if self.hideOnTimeout: self.hide() self.reset()