|
由pyskydrive工程(http://code.google.com/p/pyskydrive/)中发现的进度条,加了一点修改!
import osimport sysimport cmdimport unicodedatafrom threading import Threadfrom time import sleepclass ProgressBar(Thread):"""A simple text progress bar control."""def __init__(self):self.running = Trueself.percent = -1self.msg = ""Thread.__init__(self)def run(self):i = 1while self.running and self.percent < 100:display = '/r'if self.msg != "": display += self.msg + ' 'if self.percent >= 0:display += str(self.percent) + '% 'display += '-' * (40 * self.percent / 100)if i + 1 + len(display) > 80: i = 1display += '-' * idisplay += '>'display += ' ' * (80 - len(display))sys.stderr.write(display)i += 1sleep(0.5)sys.stderr.write('/r' + ' ' * 79 + '/r')sys.stderr.flush()def stop(self):"""Stop displaying progress bar.Note: there may be latency to stop. You'd better wait for the threadstops. See _stop_progress(t_bar)."""self.running = Falsedef set_percent(self, percent, msg = ""):"""Call back method for owner of progress bar."""self.percent = int(percent)self.msg = msgdef is_alive(self):return self.isAlive()def _start_progress():"""Display a progress bar"""t_bar = ProgressBar()t_bar.start()return t_bar def _stop_progress(t_bar):"""Hide the progress bar"""if hasattr(t_bar, "is_alive"):alive = t_bar.is_alive()else:alive = t_bar.isAlive()if t_bar and alive:t_bar.stop()t_bar.join()def _set_progress(t_bar,percent,msg = ""):t_bar.set_percent(percent)def _print_msg(t_bar, *args):"""Hide the progress bar and print message"""assert t_bar is None or isinstance(t_bar, ProgressBar)if t_bar and t_bar.is_alive(): _stop_progress(t_bar)print ' '.join(args)def _unicode_str_width(text):"""Calculate the exact width of unicode string."""width = 0for char in text:if isinstance(char, unicode):if unicodedata.east_asian_width(char) in ('F', 'W', 'A'):width += 2else:width += 1else:width += 1return width#Test the progress bar if __name__ == "__main__":t_bar = _start_progress()count = 0;while count<100:_set_progress(t_bar,count,'%d'%(count))sleep(0.1)count = count+1_stop_progress(t_bar)print "Done!"
*其实几个控制函数还可以封装到一个类中使用,那就更为方便了! |
|
|