1 from Tkinter import * 2 import ttk 3 import threading 4 import random 5 import time 6 7 class PgBarFrm(Frame): 8 def __init__(self, root, max_len): 9 self.root = root 10 Frame.__init__(self, root) 11 12 self.pb_val = IntVar() # pbar variable 13 self.pb_max = max_len # pbar maximum 14 15 self.lb_str = StringVar() # label string 16 self.update_label() 17 18 self.pack(side=TOP, expand=1, fill=X) 19 20 self.initUI() 21 22 def initUI(self): 23 self.label = Label(self, textvariable=self.lb_str) 24 self.label.pack(side=LEFT) 25 26 self.pbar = ttk.Progressbar(self, length=300, maximum=self.pb_max, variable=self.pb_val) 27 self.pbar.pack(side=LEFT, expand=1, fill=BOTH) 28 29 def update_label(self): 30 self.lb_str.set('%d/%d' % (self.pb_val.get(), self.pb_max)) 31 32 def clear(self): 33 self.pb_val.set(0) 34 self.update_label() 35 36 def step(self): 37 new_val = (self.pb_val.get()+1) 38 if new_val>self.pb_max: 39 self.clear() 40 self.step() 41 else: 42 self.pb_val.set(new_val) 43 self.update_label() 44 45 46 47 class DualWork: 48 def __init__(self): 49 self.N1 = 5 50 self.N2 = 7 51 self.frm1 = PgBarFrm(root, self.N1) 52 self.frm2 = PgBarFrm(root, self.N2) 53 54 def work(self): 55 for i in xrange(self.N1): 56 for j in xrange(self.N2): 57 rnd_t = random.uniform(0, 1) 58 print threading.currentThread().getName(), i, j, rnd_t 59 60 self.frm2.step() 61 time.sleep(rnd_t) 62 63 self.frm1.step() 64 65 def start(self): 66 w_td = threading.Thread(target=self.work) 67 w_td.start() 68 69 if __name__=='__main__': 70 root = Tk() 71 72 w = DualWork() 73 w.start() 74 75 root.mainloop()