pyqt6如何写进度条
时间: 2024-04-29 13:25:23 浏览: 137
使用 PyQt6 中的 QProgressBar 组件可以轻松地创建进度条。
以下是创建进度条的示例代码:
```python
from PyQt6.QtWidgets import QApplication, QWidget, QProgressBar, QPushButton, QVBoxLayout
from PyQt6.QtCore import Qt, QTimer
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.progress_bar = QProgressBar()
self.progress_bar.setMinimum(0)
self.progress_bar.setMaximum(100)
self.progress_bar.setValue(0)
self.start_button = QPushButton('Start')
self.start_button.clicked.connect(self.startProgressBar)
layout = QVBoxLayout()
layout.addWidget(self.progress_bar)
layout.addWidget(self.start_button)
self.setLayout(layout)
def startProgressBar(self):
self.timer = QTimer()
self.timer.timeout.connect(self.updateProgressBar)
self.timer.start(100)
def updateProgressBar(self):
value = self.progress_bar.value() + 1
if value > 100:
self.timer.stop()
return
self.progress_bar.setValue(value)
if __name__ == '__main__':
app = QApplication([])
widget = MyWidget()
widget.show()
app.exec()
```
在这个示例代码中,我们首先创建了一个 QProgressBar 组件,并设置了它的最小值、最大值和当前值。然后创建了一个 QPushButton,当用户单击它时,就会启动一个 QTimer,每隔一段时间就会更新进度条的值。当进度条的值达到最大值时,就停止 QTimer。最后,我们将这两个组件添加到 QVBoxLayout 中,并将其设置为窗口的布局。
阅读全文
相关推荐


















