q_progress_bar.md 7.1 KB
Newer Older
F
feilong 已提交
1 2
# Python PyQt 进度条(2)

F
feilong 已提交
3
![](https://gitcode.net/csdn/skill_tree_python/-/raw/master/data/2.python%E4%B8%AD%E9%98%B6/4.%E6%A1%8C%E9%9D%A2%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/2.PyQT/q_progress_bar.png)
F
feilong 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

请先完成上一题。封装一个能显示进度条的 PyQT GUI 对话框, 对话框功能如下:

1. 点击按钮“看资料+做题,就有技能树进度” 进度条开始步进
2. 每次步进,进度条往前走1步,标题显示“Python技能树->PyQT:{i}/5”
3. 一共5步,运行过程中按钮不能点击,运行结束可以重新点击

```python
# -*- coding: UTF-8 -*-
import sys
import time
import math
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWidgets import QPushButton, QProgressBar
from PyQt5.QtWidgets import QVBoxLayout, QMessageBox
from q_progress_thread import QProgressThread

class QRunnerProgressBar(QWidget):
    def __init__(self, count, tip, runner):
        super(QRunnerProgressBar, self).__init__()
        self.setWindowTitle('QRunnerProgressBar')

        self.vbox = QVBoxLayout()

        self.btn = QPushButton(tip)
        self.btn.clicked.connect(self.on_btn_click)
        self.vbox.addWidget(self.btn)

        self.pbar = QProgressBar(self)
        self.pbar.setValue(0)
        self.vbox.addWidget(self.pbar)

        self.setLayout(self.vbox)
        self.resize(300, 100)
        self.show()

        self.count = count
        self.runner = runner

    def on_btn_click(self):
        # 复用上一题的 QProgressThread
        self.thread = QProgressThread(self.runner)
        self.thread.connect(self.on_progress)
        self.thread.start()
        self.btn.setEnabled(False)

    def on_progress(self, info):
        percent_value = int(math.floor(info.progress*100/self.count))
        self.pbar.setValue(int(percent_value))
        self.setWindowTitle(info.msg)
        if self.pbar.value() == self.count:
            self.pbar.setValue(0)
            self.btn.setEnabled(True)

    def closeEvent(self, event):
        self.thread = None


if __name__ == "__main__":
    app = QApplication(sys.argv)

    def on_run(emit_progress):
        for i in range(0, 5):
            time.sleep(1)
            emit_progress(i+1, f'Python技能树->PyQT:{i+1}/5')

    p = QRunnerProgressBar(
        count=5,
        tip='看资料+做题,就有技能树进度',
        runner=on_run
    )
    sys.exit(app.exec_())
```

请选出下列能**正确**实现这一功能的选项。

## template

```python
import sys
import time
import math
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWidgets import QPushButton, QProgressBar
from PyQt5.QtWidgets import QVBoxLayout, QMessageBox
from q_progress_thread import QProgressThread

class QRunnerProgressBar(QWidget):
    def __init__(self, count, tip, runner):
        super(QRunnerProgressBar, self).__init__()
        self.setWindowTitle('QRunnerProgressBar')

        self.vbox = QVBoxLayout()

        self.btn = QPushButton(tip)
        self.btn.clicked.connect(self.on_btn_click)
        self.vbox.addWidget(self.btn)

        self.pbar = QProgressBar(self)
        self.pbar.setValue(0)
        self.vbox.addWidget(self.pbar)

        self.setLayout(self.vbox)
        self.resize(300, 100)
        self.show()

        self.count = count
        self.runner = runner

    def on_btn_click(self):
        self.thread = QProgressThread(self.runner)
        self.thread.connect(self.on_progress)
        self.thread.start()
        self.btn.setEnabled(False)

    def on_progress(self, info):
        percent_value = int(math.floor(info.progress*100/self.count))
        self.pbar.setValue(int(percent_value))
        self.setWindowTitle(info.msg)
        if self.pbar.value() == 100:
            self.pbar.setValue(0)
            self.btn.setEnabled(True)
            self.setWindowTitle('QRunnerProgressBar')

    def closeEvent(self, event):
        self.thread = None


if __name__ == "__main__":
    app = QApplication(sys.argv)

    def on_run(emit_progress):
        for i in range(0, 5):
            time.sleep(1)
            emit_progress(i+1, f'Python技能树->PyQT:{i+1}/5')

    p = QRunnerProgressBar(
        count=5,
        tip='看资料+做题,就有技能树进度',
        runner=on_run
    )
    sys.exit(app.exec_())
```

## 答案

```python
class QRunnerProgressBar(QWidget):
    ...
    def on_btn_click(self):
        # 复用上一题的 QProgressThread
        self.thread = QProgressThread(self.runner)
        self.thread.connect(self.on_progress)
        self.thread.start()
        self.btn.setEnabled(False)

    def on_progress(self, info):
        percent_value = int(math.floor(info.progress*100/self.count))
        self.pbar.setValue(int(percent_value))
        self.setWindowTitle(info.msg)
        if self.pbar.value() == 100:
            self.pbar.setValue(0)
            self.btn.setEnabled(True)
            self.setWindowTitle('QRunnerProgressBar')
```

## 选项

### A

```python
class QRunnerProgressBar(QWidget):
    ...
    def on_btn_click(self):
        # 复用上一题的 QProgressThread
        self.thread = QProgressThread(self.runner)
        self.thread.connect(self.on_progress)
        self.thread.start()
        self.btn.setEnabled(False)

    def on_progress(self, info):
        percent_value = int(math.floor(info.progress*100/self.count))
        self.pbar.setValue(int(percent_value))
        self.setWindowTitle(info.msg)
```

### B

```python
class QRunnerProgressBar(QWidget):
    ...
    def on_btn_click(self):
        # 复用上一题的 QProgressThread
        self.thread = QProgressThread(self.runner)
        self.thread.connect(self.on_progress)
        self.thread.start()
        self.btn.setEnabled(False)

    def on_progress(self, info):
        percent_value = int(math.floor(info.progress*100/self.count))
        self.pbar.setValue(int(percent_value))
        self.setWindowTitle(info.msg)
        if self.pbar.value() == self.count:
            self.pbar.setValue(0)
            self.btn.setEnabled(True)
            self.setWindowTitle('QRunnerProgressBar')
```

### C

```python
class QRunnerProgressBar(QWidget):
    ...
    def on_btn_click(self):
        # 复用上一题的 QProgressThread
        self.thread = QProgressThread(self.runner)
        self.thread.connect(self.on_progress)
        self.thread.start()
        self.btn.setEnabled(False)

    def on_progress(self, info):
        self.pbar.setValue(info.progress)
        self.setWindowTitle(info.msg)
        if self.pbar.value() == 100:
            self.pbar.setValue(0)
            self.btn.setEnabled(True)
            self.setWindowTitle('QRunnerProgressBar')
```

### D

```python
class QRunnerProgressBar(QWidget):
    ...
    def on_btn_click(self):
        # 复用上一题的 QProgressThread
        self.thread = QProgressThread(self.runner)
        self.thread.connect(self.on_progress)
        self.thread.start()

    def on_progress(self, info):
        percent_value = int(math.floor(info.progress*100/self.count))
        self.pbar.setValue(int(percent_value))
        self.setWindowTitle(info.msg)
        if self.pbar.value() == 100:
            self.pbar.setValue(0)
            self.setWindowTitle('QRunnerProgressBar')
```