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
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Exemple de classe permettant dutiliser une QProgressBar avec un itérable.
:module: iterableQProgressBar
:author: Yoann LE BARS
:version: 1.0
:date: 16/08/2018
Copyright © 2018 Le Bars, Yoann
This program is distributed under CeCLL-B license, it can be copied and
modified freely as long as initial author is cited. Complete text
of CeCILL licence can be found on-line:
<http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html>
"""
import sys
from PyQt5.QtWidgets import QWidget, QProgressBar, QApplication, QPushButton
class ProgressBar (QWidget):
"""
Class providing an easy way to use QProgressBar
"""
def __init__ (self, title, minValue = 0, maxValue = None):
"""
Class initialiser.
:param title: Window title.
:param minValue: Minimum value in iteration.
:param maxValue: Maximum value in iteration.
"""
super().__init__()
self.setWindowTitle(title)
self.setGeometry(300, 300, 210, 35)
# Actual progress bar.
self._pbar = QProgressBar(self)
self._pbar.setGeometry(30, 40, 200, 25)
# Iterable through which iterate.
self._iterable = None
# Minimum iteration value.
self._minValue = minValue
self._pbar.setMinimum(self._minValue)
# Maximum iteration value.
self._maxValue = maxValue
# Current iteration value.
self._currValue = self._minValue
# Whether or not iteration has started.
self._started = False
def __call__ (self, iterable, maxValue = None):
"""
Use a ProgressBar to iterate through an iterable.
:param iterable: The iterable through which iterate.
:param maxValue: Maximum value in iteration.
:return: A pointer to current instance.
"""
if maxValue is None:
self._maxValue = len(iterable)
else:
self._maxValue = maxValue
self._pbar.setMaximum(self._maxValue)
self._pbar.setValue(self._currValue)
self._iterable = iter(iterable)
self.show()
return self
def __iter__ (self):
"""
Defines the class as iterable.
:return: A pointer to current instance.
"""
return self
def __next__ (self):
"""
What to be done every next iteration.
"""
try:
# New value in iteration.
value = next(self._iterable)
if self._started:
self._currValue = self._currValue + 1
else:
self._started = True
self._currValue = self._minValue
self._pbar.setValue(self._currValue)
return value
except StopIteration:
raise
def __exit__ (self, exc_type, exc_value, traceback):
"""
What to do when exiting ProgressBar.
:param exc_type: Type of execution.
:param exc_value: Execution code.
:param traceback: Execution trace.
"""
pass
def __enter__ (self):
"""
What to do when entering ProgressBar.
:return: A pointer to current instance.
"""
return self
class Exemple (QWidget):
"""
Classe pour la fenêtre principale dexemple.
"""
def _realiseAction (self):
"""
Que faire lorsque le bouton est appuyé.
"""
# Iterable pour test.
liste = [valeur for valeur in range(100000)]
# Barre de progression indiquant létat davancement dans le fichier.
progression = ProgressBar('Lecture de la liste')
for valeur in progression(liste):
print(valeur)
def __init__ (self):
"""
Initialisation de la fenêtre.
"""
super().__init__()
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Test de la barre de progression.')
# Bouton pour lancer lactivité.
self._btn = QPushButton('Démarrer', self)
self._btn.move(40, 80)
self._btn.clicked.connect(self._realiseAction)
self.show()
if __name__ == '__main__':
# Instance de lapplication.
app = QApplication(sys.argv)
# Fenêtre de test.
ex = Exemple()
sys.exit(app.exec_()) |
Partager