|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Created on 2019年3月8日 |
| 6 | +@author: Irony |
| 7 | +@site: https://pyqt5.com https://github.com/892768447 |
| 8 | +@email: 892768447@qq.com |
| 9 | +@file: Threading.QtThreading |
| 10 | +@description: |
| 11 | +""" |
| 12 | +from threading import Thread |
| 13 | +from time import sleep |
| 14 | + |
| 15 | +from PyQt5.QtCore import QObject, pyqtSignal, QTimer, Qt |
| 16 | +from PyQt5.QtWidgets import QWidget, QVBoxLayout, QProgressBar |
| 17 | + |
| 18 | + |
| 19 | +__Author__ = """By: Irony |
| 20 | +QQ: 892768447 |
| 21 | +Email: 892768447@qq.com""" |
| 22 | +__Copyright__ = 'Copyright (c) 2019 Irony' |
| 23 | +__Version__ = 1.0 |
| 24 | + |
| 25 | + |
| 26 | +class _Signals(QObject): |
| 27 | + |
| 28 | + updateProgress = pyqtSignal(int) |
| 29 | + |
| 30 | + |
| 31 | +Signals = _Signals() |
| 32 | + |
| 33 | + |
| 34 | +class UpdateThread(Thread): |
| 35 | + |
| 36 | + def run(self): |
| 37 | + self.i = 0 |
| 38 | + for i in range(101): |
| 39 | + self.i += 1 |
| 40 | + Signals.updateProgress.emit(i) |
| 41 | + sleep(1) |
| 42 | + self.i = 0 |
| 43 | + Signals.updateProgress.emit(i) |
| 44 | + |
| 45 | + |
| 46 | +class Window(QWidget): |
| 47 | + |
| 48 | + def __init__(self, *args, **kwargs): |
| 49 | + super(Window, self).__init__(*args, **kwargs) |
| 50 | + self.resize(400, 400) |
| 51 | + layout = QVBoxLayout(self) |
| 52 | + self.progressBar = QProgressBar(self) |
| 53 | + layout.addWidget(self.progressBar) |
| 54 | + Signals.updateProgress.connect( |
| 55 | + self.progressBar.setValue, type=Qt.QueuedConnection) |
| 56 | + |
| 57 | + QTimer.singleShot(2000, self.doStart) |
| 58 | + |
| 59 | + def doStart(self): |
| 60 | + self.updateThread = UpdateThread(daemon=True) |
| 61 | + self.updateThread.start() |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == '__main__': |
| 65 | + import sys |
| 66 | + from PyQt5.QtWidgets import QApplication |
| 67 | + app = QApplication(sys.argv) |
| 68 | + w = Window() |
| 69 | + w.show() |
| 70 | + sys.exit(app.exec_()) |
0 commit comments