使用PyQt5中的按鈕以及線程完成開始,暫停,恢復(fù),終止(完成)
示例代碼如下:
from __init__ import *class Thread(QThread):
valueChange = pyqtSignal(int)
_isFinished = pyqtSignal(bool)
def __init__(self, *args, **kwargs):
super(Thread, self).__init__(*args, **kwargs)
self.progress_value = 0
self.counter = 0
self._isPause = False # 是否暫停
self.is_running = True # 標(biāo)志位表示線程是否需要退出
self.finished = False
self.cond = QWaitCondition()
self.mutex = QMutex()
def pause(self):
# 暫停(掛起)
# 狀態(tài)改為暫停
self._isPause = True
# 恢復(fù)
def resume(self):
# 狀態(tài)改為恢復(fù)
self._isPause = False
# 調(diào)用QWaitCondition.wake()喚醒暫停的線程
self.cond.wakeAll()
def cancel(self):
# 將進度條重置為0
self.progress_value = 0
self.valueChange.emit(self.progress_value)
# 停止線程
self.finished = False
self.terminate()
def complete(self):
# 將進度條重置為0
# self.progress_value = 0
# self.valueChange.emit(self.progress_value)
# 停止線程
self.finished = False
self.terminate()
def run(self):
# 開啟線程,一直循環(huán)重復(fù)下去,監(jiān)聽狀態(tài)
self.case_Finished = False
while not self.finished:
try:
# 給線程上鎖,防止線程掛起的時候,線程中的數(shù)據(jù)狀態(tài)發(fā)生改變
self.mutex.lock()
# 如果是暫停狀態(tài),則阻塞等待
if self._isPause:
self.cond.wait(self.mutex)
if self.counter >= 100:
# self.progress_value = 0
self.counter += 1
self.progress_value = 100
self.valueChange.emit(self.progress_value)
self._isFinished.emit(True)
self.finished = True
self.mutex.unlock() # 線程恢復(fù),解鎖
if not self.finished:
# print('value: ', self.counter)
self.counter += 1
self.progress_value = self.counter
self.valueChange.emit(self.progress_value) # 發(fā)送信號,改變進度條的數(shù)據(jù)
self.msleep(20) # 休眠200 ms
self.mutex.unlock() # 線程恢復(fù),解鎖
except Exception as e:
print(f"Error: {e}")
class ThreadTest(QWidget):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.thread = None
self.progressBar = QProgressBar()
self.progressBar.setMinimumHeight(20)
self.layout.addWidget(self.progressBar)
self.buttons_widget = QWidget()
self.buttons_layout = QHBoxLayout()
self.start_cancel_btn = QPushButton('Start')
self.pause_resume_btn = QPushButton('Pause')
self.start_cancel_btn.setMaximumSize(100, 75)
self.pause_resume_btn.setMaximumSize(100, 75)
self.pause_resume_btn.setEnabled(False)
self.start_cancel_btn.clicked.connect(self.doStart)
self.pause_resume_btn.clicked.connect(self.doPause)
self.buttons_layout.addWidget(self.start_cancel_btn)
self.buttons_layout.addWidget(self.pause_resume_btn)
self.buttons_widget.setLayout(self.buttons_layout)
self.layout.addWidget(self.buttons_widget)
#==================================Start Pause Resume Cancel Button Events================================
def doStart(self):
self.start_flag = True
print('pid', os.getpid())
if self.thread is None:
# 創(chuàng)建并啟動新的線程
self.thread = Thread()
print('main id: ', int(QThread.currentThreadId()))
self.thread.valueChange.connect(self.progressBar.setValue)
self.thread._isFinished.connect(self.doComplete)
self.thread.start()
self.pause_resume_btn.setEnabled(True)
self.start_cancel_btn.setEnabled(False)
self.start_cancel_btn.setText('Cancel')
self.start_cancel_btn.clicked.disconnect()
self.start_cancel_btn.clicked.connect(self.doCancel)
# 暫停
def doPause(self):
self.start_flag = False
self.pause_flag = True
if self.thread is not None:
self.thread.pause()
self.pause_resume_btn.setText('Resume')
self.start_cancel_btn.setEnabled(True)
self.pause_resume_btn.clicked.disconnect()
self.pause_resume_btn.clicked.connect(self.doResume)
# 恢復(fù)
def doResume(self):
self.start_flag = True
self.pause_flag = False
if self.thread is not None:
self.thread.resume()
self.start_cancel_btn.setEnabled(False)
self.pause_resume_btn.setText('Pause')
self.pause_resume_btn.clicked.disconnect()
self.pause_resume_btn.clicked.connect(self.doPause)
# 完成
def doComplete(self):
# print('Test Complete')
self.start_flag = False
self.pause_flag = False
if self.thread is not None:
if self.thread.finished:
self.thread.complete()
self.thread = None
# Reset button state
self.pause_resume_btn.setEnabled(False)
self.start_cancel_btn.setEnabled(True)
self.start_cancel_btn.setText('Start')
self.start_cancel_btn.clicked.disconnect()
self.start_cancel_btn.clicked.connect(self.doStart)
# 取消
def doCancel(self):
self.start_flag = False
self.pause_flag = False
if self.thread is not None:
self.progressBar.reset()
self.thread.cancel()
self.thread = None
self.start_cancel_btn.setEnabled(True)
self.start_cancel_btn.setText('Start')
self.start_cancel_btn.clicked.disconnect()
self.start_cancel_btn.clicked.connect(self.doStart)
self.pause_resume_btn.setText('Pause')
self.pause_resume_btn.clicked.disconnect()
self.pause_resume_btn.clicked.connect(self.doPause)
def ThreadTest_Test():
app = QApplication(sys.argv)
thread_test = ThreadTest()
thread_test.setWindowTitle('Thread Test')
thread_test.show()
sys.exit(app.exec_())if __name__ == '__main__':
ThreadTest_Test()```
- 1
- 2
- 3
- 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