#!/usr/bin/env python # [http://mail.python.org/pipermail/python-list/2004-March/210555.html] from Tkinter import * import os, sys class Meter(Frame): '''A simple progress bar widget.''' def __init__(self, master, fillcolor='cyan', text='', value=0.0, **kw): self.width=350 self.height=20 Frame.__init__(self, master, bg='lightpink', width=self.width, height=self.height) self.configure(**kw) self._c = Canvas(self, bg=self['bg'], width=self['width'], height=self['height'], highlightthickness=0, relief='flat', bd=0) self._c.pack(fill='x', expand=1) self._r = self._c.create_rectangle(0, 0, 0, int(self['height']), fill=fillcolor, width=0) self._t = self._c.create_text(int(self['width'])/2, int(self['height'])/2, text='') self.set(value, text) self.pack() def set(self, value=0.0, text=None): if value < 0.0: value = 0.0 elif value > 1.0: value = 1.0 if text == None: text = '' text = text + str(int(round(100 * value))) + '%' try: self._c.coords(self._r, 0, 0, int(self['width']) * value, int(self['height'])) self._c.itemconfigure(self._t, text=text) self.master.update() except TclError: pass # Default behavior: parse progress from pipe viewer # [http://www.ivarch.com/programs/pv.shtml] if __name__=="__main__": baseText='' if len(sys.argv) > 1: baseText = sys.argv[1]+': ' value = 0.0 buf="" p = Meter(master=Tk()) currText = baseText p.set (value=value, text=currText) while 1: char = sys.stdin.read(1) if char == '': break buf = buf + char if buf[-15:-12] == 'ETA': buf = "" elif buf[-3:] == '/s]': currText = baseText + buf + ': ' elif buf[-1] == '%': value=float(buf[-3:-1])/100 p.set (value=value, text=currText)