pyqt 实现进度条窗口

时间:2021-10-20 23:02:00
 1 # -*- coding: utf-8 -*-
 2 from Mainform import Ui_Frame
 3 
 4 import sys
 5 from PyQt5.QtWidgets import QApplication, QMainWindow,QFrame,QProgressDialog,QMessageBox,QWidget
 6 from PyQt5.QtCore import QThread,pyqtSignal,QTime
 7 
 8 class ProgressDg(QProgressDialog):
 9     def __init__(self):
10         super().__init__()
11         self.setModal(True)
12         self.setWindowTitle("正在执行...")
13         self.setAutoClose(True)
14 
15 class UIForm(Ui_Frame):
16     def __init__(self):
17         self.initUI()
18         self.i = 0
19 
20     def initUI(self):
21         self.frame = QFrame()
22         super().setupUi(self.frame)
23         self.progressBar.hide()
24         self.pushButton.clicked.connect(self.work)
25         self.pushButton_2.clicked.connect(self.work)
26         self.workThread=None
27         self.frame.setVisible(True)
28 
29     def hide(self):
30         self.workThread.stop()
31         self.process.hide()
32         widget = QWidget()
33         QMessageBox.information(widget,"sss11","ssssaaa")
34 
35     def showDg(self):
36         widget = QWidget()
37         QMessageBox.information(widget,"sss","ssssaaa")
38 
39     def work(self):
40         self.i +=1
41         self.workThread=WorkThread()
42         self.process = ProgressDg()
43         self.process.canceled.connect(self.hide)
44         self.workThread.trigger.connect(self.changProgressBar)
45 
46 
47         self.workThread.start()
48         self.workThread.finished.connect(self.showDg)
49 
50     def changProgressBar(self,value):
51         if value==-1:
52             self.process.hide()
53         self.process.setValue(value)
54 
55 class WorkThread(QThread):
56 
57     trigger = pyqtSignal(int)
58     def __init__(self):
59         super(WorkThread,self).__init__()
60         self.stopFlag=0
61 
62     def run(self):
63         for i in range(101):
64             if self.stopFlag ==-1:
65                 self.trigger.emit(-1)
66                 break
67             import time
68             time.sleep(0.1)
69             self.trigger.emit(i)
70         self.exit(0)
71 
72     def stop(self):
73         self.stopFlag=-1
74 
75 
76 if __name__ == '__main__':
77     app = QApplication(sys.argv)
78     ui = UIForm()
79     sys.exit(app.exec_())