PyQt5: chapter8-使用与锁定机制绑定的线程更新进度条

实例

  1. 创建基于Dialog without Buttons模板窗口
  2. 添加Label,Process Bar各两个
  3. 设定Label的text为Downloading the file和Scanning for Virus
  4. 设定Process Bar的objectName为progressBarFileDownload和progressBarVirusScan
  5. 设定Process Bar的默认值为0
  6. 保存为demoTwoProgressBarsLocks.ui
  7. 使用pyuic生成demoTwoProgressBarsLocks.py
  8. 创建callProgressBarTwoThreadsLocks.py,代码如下
import sys
import threading
import time
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200503.demoTwoProgressBarsLocks import *

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)
        self.show()
class myThread(threading.Thread):
    counter=0
    def __init__(self,w,ProgressBar):
        threading.Thread.__init__(self)
        self.w=w
        self.counter=0
        self.progressBar=ProgressBar
    def run(self):
        print("Starting "+self.name+"n")
        threadLock.acquire()
        while self.counter<=100:
            time.sleep(1)
            self.progressBar.setValue(self.counter)
            self.counter+=10
            print("Exiting " + self.name + "n")
        threadLock.release()
if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    thread1=myThread(w,w.ui.progressBarFileDownload)
    thread2=myThread(w,w.ui.progressBarVirusScan)
    threadLock=threading.Lock()
    threads=[]
    thread1.start()
    thread2.start()
    w.exec()
    threads.append(thread1)
    threads.append(thread2)
    for t in threads:
        t.join()
    sys.exit(app.exec())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值