zoukankan      html  css  js  c++  java
  • Python使用进度条表示工作进度

    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...')
     
    

      

  • 相关阅读:
    php jquery+ajax写批量删除
    thinkphp 验证码
    thinkphp 框架中的一部分方法解析
    ThinkPHP框架的增删改
    ThinkPHP 框架模型
    ThinkPHP框架之空控制器
    江苏省盐城市响水县运河镇开源路商业街商铺及套间招商招租出售
    苏州鼎耀文化传媒有限公司
    怎么给罗技K480 增加Home、End键
    C#实现APK自动打包
  • 原文地址:https://www.cnblogs.com/nn0p/p/3040042.html
Copyright © 2011-2022 走看看