`
hanyh
  • 浏览: 235424 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

进度条class

阅读更多
#!/usr/bin/env python
'''
    NAME: progressBar.py
    AUTHOR: Augix
    DATE: 2009-05-20
 
    FUNCTION: this module prints a progress bar to show how much time you have to wait until the program finish.
    It simply reads a percentage to update the progress bar.
 
    USAGE: from progressBar import progressBar
    bar = progressBar(min=0, max=200, barLength=None, output=sys.stderr)
    for i in range(200):
        bar.update(i)
        time.sleep(0.1)
 
    Reference: http://code.activestate.com/recipes/168639/
'''

import sys, time
 
# === variables ===
 
# === functions ===
 
# === classes ===
class progressBar:
    def __init__(self, min = 0, max = 100, barLength = None, output=sys.stderr):
        self.output = output
        self.progBar = "[]"   # This holds the progress bar string
        self.min = min
        self.max = max - 1
        self.span = self.max - self.min + 1
        self.barLength = self.calBarLength(barLength)
        self.percentDone = 0
        self.timeStart = time.time()
        self.timePrevious = self.timeStart
        self.timePass = 0
        self.finish = False
        self.update(0)  # Build progress bar string
 
    def calBarLength(self, barLength):
        if barLength == None:
            from fcntl import ioctl
            from termios import TIOCGWINSZ
            from array import array
            a = ioctl(self.output,TIOCGWINSZ,'\0'*8)
            h,w=array('h',a)[:2]
            return w
        else: return barLength
 
    def calPercent(self, newAmount):
        diffFromMin = float(newAmount - self.min + 1)
        percentDone = (diffFromMin / float(self.span)) * 100.0
        return percentDone
 
    def calWaitingTime(self):
        self.timePass = time.time() - self.timeStart
        timeWait = self.timePass / float(self.percentDone) * (100.0 - self.percentDone)
        return timeWait
 
    def formatTime(self, seconds):
        seconds = int(round(seconds))
        hour = seconds / 3600
        if hour == 0: hour = ""
        else: hour = " " + str(hour) + " hour"
        minute = seconds % 3600 / 60
        if minute == 0: minute = ""
        else: minute = " " + str(minute) + " min"
        second = seconds % 60
        second = " " + str(second) + " sec"
        s = hour + minute + second
        return s
 
    def formatPercent(self, percent):
        i = str(int(round(percent)))
        s = " "*(3-len(i)) + i + "%"
        return s
 
    def update(self, newAmount = 0):
        timeNow = time.time()
        if timeNow - self.timePrevious < 0.5:
            if newAmount < self.max: return None
        self.timePrevious = timeNow
        # Calculate the percentage finished
        if newAmount < self.min: newAmount = self.min
        if newAmount >= self.max: newAmount = self.max; self.finish = True;
        self.percentDone = self.calPercent(newAmount)
        printPercent = self.formatPercent(self.percentDone)
 
        # Calculate the waiting time
        if self.percentDone == 0: printETA = ""
        elif self.finish == True: printETA = self.formatTime(self.timePass) + " used"
        else:
            timeWait = self.calWaitingTime()
            printETA = self.formatTime(timeWait) + " left"
 
        # Figure out how many '=' and spaces the bar should be
        spaceAndEqual = self.barLength - 11 - len(printETA)
        numEqual = (self.percentDone / 100.0) * spaceAndEqual
        numEqual = int(round(numEqual))
 
        # the rotating thing
        animation = '\|/-'
        printAnimation = animation[int(timeNow % 4)]
 
        # build a progress bar with "=" and spaces
        self.progBar = " " + printPercent + " [" + printAnimation + '='*numEqual + ">" + ' '*(spaceAndEqual-numEqual) + "] " + printETA
        if self.finish == True:
            self.output.write(self.progBar + "\n")
        else:
            self.output.write(self.progBar + "\r")
 
    def __str__(self):
        return str(self.progBar)
 
# === Main ===
if __name__ == '__main__':
    bar = progressBar(max=200)
    for i in range(200):
        bar.update(i)
        time.sleep(0.1)
分享到:
评论

相关推荐

    DataGridView进度条 DataGridView进度条 C#

    public class ProgressBarColumn : DataGridViewColumn { // ... protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds,...

    BS 进度条 很漂亮的进度条

    在IT行业中,尤其是在网页开发和用户界面设计领域,进度条是一种不可或缺的元素,它能够向用户提供操作进度的视觉反馈,增强用户体验。标题提到的“BS进度条”很可能是指基于Bootstrap框架的进度条组件。Bootstrap是...

    form表单填写进度条

    2.将进度条展示div放进页面,最好放在顶部: &lt;div class="top"&gt;&lt;div class="colors"&gt;&lt;/div&gt;&lt;/div&gt; 3.调用js函数 [removed] flash(); [removed] 4.在form标签添加class属性: class="form" 即可;

    Qt重写QWidget实现按钮进度条

    class ProgressButton : public QPushButton { Q_OBJECT public: ProgressButton(QWidget *parent = nullptr) : QPushButton(parent) {} protected: void paintEvent(QPaintEvent *) { QPainter painter(this)...

    c# DataGridView添加进度条

    在某些场景下,我们可能需要在`DataGridView`的某个列中显示进度条,以实时反映任务的执行状态。这就涉及到自定义`DataGridView`列类型和使用进度条控件(如`ProgressBar`)的知识。以下将详细讲解如何实现这个功能...

    winform竖向进度条

    public class VerticalProgressBar : ProgressBar { protected override void OnPaint(PaintEventArgs e) { // 根据当前进度计算高度 int height = (int)(((double)Value / Maximum) * ClientRectangle.Height...

    C#winform条形方格进度条,横竖进度条

    public class CustomProgressBar : Control { // 控件属性:如底色、进度色、是否显示进度值等 private Color backColor; private Color progressColor; private bool showProgressText; private int fontSize;...

    bootStrap进度条

    这里,`&lt;div class="progress"&gt;`是进度条容器,`&lt;div class="progress-bar"&gt;`则是实际的进度条元素。`aria-*`属性用于无障碍访问,确保屏幕阅读器可以正确解读进度条的状态。 ### 2. 颜色和样式 Bootstrap提供了...

    Java实现显示进度条

    public class ProgressBa extends Thread implements ActionListener { ``` `ProgressBa`r类继承自`Thread`并实现了`ActionListener`接口。这意味着它既可以作为一个线程独立运行,又可以监听事件并作出响应。 3...

    pyside2 显示进度条

    在PySide2中,显示进度条是用户界面(UI)编程的一个重要方面,尤其是在执行耗时操作时,为用户提供反馈是非常必要的。PySide2是Qt库的Python绑定,提供了丰富的UI组件和功能,包括进度条。QThread是Qt中的一个关键...

    css3进度条

    这里的`&lt;div class="progress-bar"&gt;`是进度条容器,而`&lt;div class="progress"&gt;`表示实际的进度部分,通过设置`style="width: 75%"`来控制进度的百分比。 接下来,我们使用CSS来定制进度条的样式。基础样式可能如下...

    基于taro微信小程序环形进度条

    通过Taro的Class和Style绑定,可以轻松实现对环形进度条的样式定制,如颜色、大小、边框宽度等。同时,还可以添加点击事件,让环形进度条具有交互性,如点击后刷新进度。 7. **性能优化**: 在大量使用环形进度条...

    php进度条php进度条php进度条php进度条php进度条

    本文将详细讲解PHP实现进度条的相关知识点,并结合提供的文件名`class.progress_bar.php`、`example.php`以及`PHP100说明.txt`进行说明。 首先,我们要理解PHP进度条的基本原理。由于PHP是服务器端脚本语言,它无法...

    WPF实现的圆形进度条

    在Windows Presentation Foundation (WPF) 中,开发人员可以利用丰富的图形功能来创建各种自定义UI元素,其中之一就是圆形进度条。圆形进度条通常用于展示一个任务的进度,它以直观的方式呈现百分比完成状态,与传统...

    VC++进度条控件视频

    在VC++编程环境中,进度条控件(Progress Bar Control)是一个不可或缺的元素,它用于向用户展示程序执行的进度,从而提高用户体验。本教程通过“VC++进度条控件视频”将深入讲解如何在MFC(Microsoft Foundation ...

    kotlin实现的进度条

    class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val progressBar = ...

    API时钟进度条.rar

    1. **CreateWindowEx**:创建一个窗口,这个窗口可以是进度条控件(Class: "PB_CLASS")或者一个包含时钟的子窗口。 2. **SetWindowLong/GetWindowLong**:用于设置或获取窗口的额外风格和属性,例如设置进度条的...

    MFC基于Progress的无限滚动进度条

    ProgressCtrl是MFC为Windows进度条控件(HWND_CLASS_PROGRESS)提供的封装。它允许程序员创建、修改和控制进度条的外观和行为。通过继承CProgressCtrl类,我们可以轻松地在对话框、视图或其他窗口中添加进度条控件。...

    通过js实现百分比进度条

    在网页设计中,进度条是一种常见的用户界面元素,它能够直观地展示任务的完成状态或进程。本篇文章将深入探讨如何使用JavaScript(JS)、HTML和CSS来实现一个动态的百分比进度条。首先,我们需要理解这三种技术在...

    fileupload多种进度条模式

    "fileupload多种进度条模式"这个主题聚焦于如何为文件上传提供美观且实用的进度反馈机制。本文将详细介绍几种常见的进度条模式以及它们的实现原理。 1. **基础HTML5 Progress Element** HTML5引入了`&lt;progress&gt;`...

Global site tag (gtag.js) - Google Analytics