PyQt5: chapter8-使用线程更新进度条

threading 模块方法

  • threading.activeCount():此方法返回当前活动线程对象的数目
  • threading.currentThread():此方法返回当前线程对象
  • threading.enumerate():此方法返回所有当前活动线程对象的列表

Thread 类方法

  • run():此方法开始执行线程
  • start():此方法通过调用run()
  • join([time])启动线程:此方法等待线程终止
  • isAlive():此方法检查线程是否仍在执行
  • getName():此方法返回线程的名称
  • setName():此方法设置线程的名称

以下是异步编程所需的方法

  • asyncio.sleep(delay):如果发生等待调用,异步函数将暂停其执行。暂停的时间为delay
  • asyncio.get_event_loop():此方法用于获取默认事件循环()。事件循环调度并运行异步任务              
  • loop.run_until_complete():在完成所有异步任务之前,此方法不会返回

实例

  1. 创建基于Dialog without Buttons模板窗口
  2. 添加Label,Process Bar各一个
  3. 设定Label的text为Downloading the file
  4. 设定Process Bar的默认值为0
  5. 保存为demoProgressBarThread.ui
  6. 使用pyuic生成demoProgressBarThread.py
  7. 创建callProgressBar.py,代码如下
import sys
import threading
import time
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200503.demoProgressBarThread 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):
        threading.Thread.__init__(self)
        self.w=w
        self.counter=0
    def run(self):
        print("Starting "+self.name)
        while self.counter<=100:
            time.sleep(1)
            w.ui.progressBar.setValue(self.counter)
            self.counter+=10
            print("Exiting "+self.name)
if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    thread1=myThread(w)
    thread1.start()
    w.exec()
    sys.exit(app.exec())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值