实例
创建基于Dialog without Buttons模板窗口 添加Label,Process Bar各两个 设定Label的text为Downloading the file和Scanning for Virus 设定Process Bar的objectName为progressBarFileDownload和progressBarVirusScan 设定Process Bar的默认值为0 添加Push Button部件,其text为Start,objectName为pushButtonStart 保存为demoTwoProgressBarsAsync.ui 使用pyuic生成demoTwoProgressBarsAsync.py 创建callProgressBarAsync1.py,代码如下
import sys
import asyncio
from PyQt5.QtWidgets import QDialog,QApplication
from quamash import QEventLoop
from cookbook_200503.demoTwoProgressBarsAsync import *
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui=Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButtonStart.clicked.connect(self.invokeAsync)
self.show()
def invokeAsync(self):
asyncio.ensure_future(self.updt(0.5,self.ui.progressBarFileDownload))
asyncio.ensure_future(self.updt(1,self.ui.progressBarVirusScan))
@staticmethod
async def updt(delay,ProgressBar):
for i in range(101):
await asyncio.sleep(delay)
ProgressBar.setValue(i)
def stopper(loop):
loop.stop()
if __name__=="__main__":
app=QApplication(sys.argv)
loop=QEventLoop(app)
asyncio.set_event_loop(loop)
w=MyForm()
w.exec()
with loop:
loop.run_forever()
loop.close()
sys.exit(app.exec())