用Python语言写一个pyqt5的与算法相关的进度条功能
时间: 2023-05-15 14:05:56 浏览: 222
可以使用以下代码实现:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QProgressBar, QPushButton
from PyQt5.QtCore import QThread, pyqtSignal
class AlgorithmThread(QThread):
update_progress = pyqtSignal(int)
def __init__(self):
super().__init__()
def run(self):
for i in range(101):
self.update_progress.emit(i)
self.msleep(50)
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Algorithm Progress'
self.left = 100
self.top = 100
self.width = 400
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.progress_bar = QProgressBar(self)
self.progress_bar.setGeometry(30, 40, 350, 25)
self.start_button = QPushButton('Start', self)
self.start_button.move(150, 80)
self.start_button.clicked.connect(self.start_algorithm)
self.show()
def start_algorithm(self):
self.algorithm_thread = AlgorithmThread()
self.algorithm_thread.update_progress.connect(self.update_progress_bar)
self.algorithm_thread.start()
def update_progress_bar(self, value):
self.progress_bar.setValue(value)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
```
这个程序创建了一个窗口,其中包含一个进度条和一个开始按钮。当用户点击开始按钮时,程序会启动一个线程来模拟一个算法的运行,并通过信号来更新进度条的值。
阅读全文