返回顶部

多线程/多进程+QProgressBar实现进度条

 

多线程+QProgressBar实现进度条:

 1 import threading
 2 
 3 from PyQt5.Qt import * #刚开始学习可以这样一下导入
 4 import sys,time
 5 
 6 rate = 0
 7 class MyProcessBar(QProgressBar):
 8     def __init__(self):
 9         super().__init__()
10         self.setWindowTitle("test的学习")
11         self.resize(300,30)
12         self.set_ui()
13 
14     def set_ui(self):
15         timer = QTimer(self)
16         timer.start(100)
17         timer.timeout.connect(self.timer_timeout_slot)
18 
19     def timer_timeout_slot(self):
20         self.setValue(rate+1)
21 
22 def create_ui_show():
23     app =QApplication(sys.argv)
24     processBar = MyProcessBar()
25     processBar.show()
26     sys.exit(app.exec_())
27 
28 if __name__ == '__main__':
29     thread1 = threading.Thread(target=create_ui_show)
30     thread1.start()
31 
32     for i in range(100):
33         time.sleep(0.05)
34         rate = i
35     if i == 99:
36         print("下载完成")
View Code

 

多进程+QProgressBar实现进度条:

 1 import multiprocessing
 2 
 3 from PyQt5.Qt import * #刚开始学习可以这样一下导入
 4 import sys,time
 5 
 6 def create_ui_show(rate):
 7     app  = QApplication(sys.argv)
 8     progressBar = QProgressBar()
 9 
10     timer = QTimer()
11     def test():
12         progressBar.setValue(rate.value+1)
13         if rate.value == 99:
14             sys.exit(0)
15     timer.timeout.connect(test)
16     timer.start(100)
17 
18     progressBar.show()
19     sys.exit(app.exec_())
20 
21 if __name__ == '__main__':
22     rate = multiprocessing.Value("d",0)  #初始的rate.value = 0
23 
24     process1 = multiprocessing.Process(target=create_ui_show,args=(rate,))
25     process1.start()
26 
27     for i in range(100):
28         time.sleep(0.1)
29         rate.value = i
View Code

 

posted @ 2019-08-28 21:45  Zcb0812  阅读(1836)  评论(0编辑  收藏  举报