GUI用的是Tkinter,在主线程执行,子线程是工作线程
""" ttk.Progressbar demo, with multi-threading author: nn0p data: 2013.04.24 """ from Tkinter import * import ttk import threading import random import time class UIThread: def __init__(self, root, count=10): self.count = count self.value = 0 self.b_exit = False self.root = root self.event = threading.Event() self.pbar = ttk.Progressbar(root, length=200, maximum=count) self.pbar.pack() root.after(0, self.update) def set_max(self, max): self.pbar.config(maximum=max) def set_val(self, val): self.value = val def start(self): self.event.set() def stop(self): self.b_exit = True def update(self): print 'waiting...' self.event.wait() print 'start progress...' while 1: self.pbar.config(value=self.value) self.pbar.update() if self.b_exit: break self.root.quit() print 'root quit()' def increase(slef): self.value += 1 def work_func(count, ui): ui.set_max(count) ui.start() print '->working<-' for i in range(1, count+1): rnd_t = random.uniform(.2, .5) print i, threading.currentThread().getName(), ':', time.ctime() ui.set_val(i) time.sleep(rnd_t) ui.stop() if __name__ =='__main__': root = Tk() ui = UIThread(root) work_thread = threading.Thread(target=work_func, args=(5, ui)) work_thread.start() root.mainloop() work_thread.join() raw_input('Press return...')