diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index be3cc83..83e8587 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -65,6 +65,8 @@ encoding//\u56FE\u8868/PyQtChart/demo/LineChart\u81EA\u5B9A\u4E49xy\u8F74.py=utf encoding//\u56FE\u8868/PyQtChart/demo/ToolTip.py=utf-8 encoding//\u56FE\u8868/PyQtChart/demo/ToolTip2.py=utf-8 encoding//\u591A\u7EBF\u7A0B/moveToThread.py=utf-8 +encoding//\u591A\u7EBF\u7A0B/\u7EBF\u7A0B\u4F11\u7720\u5524\u9192.py=utf-8 +encoding//\u591A\u7EBF\u7A0B/\u7EBF\u7A0B\u6302\u8D77\u6062\u590D.py=utf-8 encoding//\u591A\u7EBF\u7A0B/\u7EE7\u627FQThread.py=utf-8 encoding//\u591A\u9875\u9762/QScrollArea/\u4EFFQQ\u8BBE\u7F6E\u9762\u677F/SettingUi.py=utf-8 encoding//\u591A\u9875\u9762/QScrollArea/\u4EFFQQ\u8BBE\u7F6E\u9762\u677F/\u4EFFQQ\u8BBE\u7F6E\u9762\u677F.py=utf-8 diff --git a/README.md b/README.md index d2acb1b..48aa39f 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站 ## [多线程](多线程) 1. [线程挂起恢复](多线程/线程挂起恢复.py) +1. [线程休眠唤醒](多线程/线程休眠唤醒.py) 1. [继承QThread](多线程/继承QThread.py) 1. [moveToThread](多线程/moveToThread.py) diff --git a/多线程/README.md b/多线程/README.md index 61d870d..a6ec646 100644 --- a/多线程/README.md +++ b/多线程/README.md @@ -16,4 +16,10 @@ PyQt多线程的简单使用例子 `ctypes.windll.kernel32.TerminateThread`终止线程,不推荐 -![截图](ScreenShot/线程挂起恢复.gif) \ No newline at end of file +![截图](ScreenShot/线程挂起恢复.gif) + +## [4、线程休眠唤醒](线程休眠唤醒.py) + +使用`QWaitCondition`的wait和wakeAll方法 + +![截图](ScreenShot/线程休眠唤醒.gif) \ No newline at end of file diff --git a/多线程/ScreenShot/线程休眠唤醒.gif b/多线程/ScreenShot/线程休眠唤醒.gif new file mode 100644 index 0000000..dbf1037 Binary files /dev/null and b/多线程/ScreenShot/线程休眠唤醒.gif differ diff --git a/多线程/线程休眠唤醒.py b/多线程/线程休眠唤醒.py new file mode 100644 index 0000000..ac60291 --- /dev/null +++ b/多线程/线程休眠唤醒.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2018年11月11日 +@author: Irony +@site: https://pyqt5.com, https://github.com/892768447 +@email: 892768447@qq.com +@file: +@description: +""" +from PyQt5.QtCore import QThread, QWaitCondition, QMutex, pyqtSignal +from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QProgressBar + + +__Author__ = """By: Irony +QQ: 892768447 +Email: 892768447@qq.com""" +__Copyright__ = 'Copyright (c) 2018 Irony' +__Version__ = 1.0 + + +class Thread(QThread): + + valueChange = pyqtSignal(int) + + def __init__(self, *args, **kwargs): + super(Thread, self).__init__(*args, **kwargs) + self._isPause = False + self._value = 0 + self.cond = QWaitCondition() + self.mutex = QMutex() + + def pause(self): + self._isPause = True + + def resume(self): + self._isPause = False + self.cond.wakeAll() + + def run(self): + while 1: + self.mutex.lock() + if self._isPause: + self.cond.wait(self.mutex) + if self._value > 100: + self._value = 0 + self._value += 1 + self.valueChange.emit(self._value) + self.msleep(100) + self.mutex.unlock() + + +class Window(QWidget): + + def __init__(self, *args, **kwargs): + super(Window, self).__init__(*args, **kwargs) + layout = QVBoxLayout(self) + self.progressBar = QProgressBar(self) + layout.addWidget(self.progressBar) + layout.addWidget(QPushButton('休眠', self, clicked=self.doWait)) + layout.addWidget(QPushButton('唤醒', self, clicked=self.doWake)) + + self.t = Thread(self) + self.t.valueChange.connect(self.progressBar.setValue) + self.t.start() + + def doWait(self): + self.t.pause() + + def doWake(self): + self.t.resume() + + +if __name__ == '__main__': + import sys + import cgitb + sys.excepthook = cgitb.enable(1, None, 5, '') + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + w = Window() + w.show() + sys.exit(app.exec_())