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
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
# Python 2.7
import sys, os
import time
import urllib
from PyQt4 import QtCore, QtGui
#############################################################################
class Monthread(QtCore.QThread):
#========================================================================
def __init__(self, source, destination, parent=None):
QtCore.QThread.__init__(self, parent)
self.source = source
self.destination = destination
self.stop = False
#========================================================================
def run(self):
# lancement du téléchargement
try:
filename, msg = urllib.urlretrieve(self.source, self.destination,
reporthook=self.infotelech)
messagefin = u"Téléchargement terminé"
except Exception:
messagefin = u"Téléchargement avorté"
# fin du thread: émission du message de fin
self.emit(QtCore.SIGNAL("finmonthread(PyQt_PyObject)"), messagefin)
#========================================================================
def infotelech(self, telechbloc, taillebloc, totalblocs):
"""reçoit les infos de progression du téléchargement"""
if self.stop:
raise Exception
# envoie les infos à la fenêtre graphique
self.emit(QtCore.SIGNAL("infotelech(PyQt_PyObject)"), [telechbloc, taillebloc, totalblocs])
#========================================================================
def stoptelech(self):
# permet l'arrêt du téléchargement avant la fin
self.stop = True
#############################################################################
class Fenetre(QtGui.QWidget):
#========================================================================
def __init__(self, parent=None):
super(Fenetre,self).__init__(parent)
self.depart = QtGui.QPushButton(u"Départ", self)
self.depart.clicked.connect(self.depart_m)
self.stop = QtGui.QPushButton(u"Stop", self)
self.stop.clicked.connect(self.stop_m)
self.barre = QtGui.QProgressBar(self)
self.barre.setMinimum = 0
self.barre.setMaximum = 100
self.barre.setValue(0)
posit = QtGui.QGridLayout()
posit.addWidget(self.depart, 0, 0)
posit.addWidget(self.stop, 1, 0)
posit.addWidget(self.barre, 2, 0)
self.setLayout(posit)
self.monthread = None
self.source = "http://docs.python.org/archives/python-2.7.1-docs-pdf-a4.zip"
self.destination = "python-2.7.1-docs-pdf-a4.zip"
#========================================================================
def depart_m(self):
if self.monthread==None or not self.monthread.isRunning():
self.barre.setValue(0)
# démarre le thread
self.monthread = Monthread(self.source, self.destination)
self.connect(self.monthread, QtCore.SIGNAL("infotelech(PyQt_PyObject)"),
self.majbarre)
self.connect(self.monthread, QtCore.SIGNAL("finmonthread(PyQt_PyObject)"),
self.finmonthread)
self.monthread.start()
#========================================================================
def majbarre(self, x):
"""lancé à chaque réception d'info sur la progression du téléchargement"""
if x[2]>0:
p = int(x[0]*x[1]/x[2]*100)
if p>100:
p = 100
self.barre.setValue(p)
QtCore.QCoreApplication.processEvents() # force le rafraichissement
else:
self.barre.setMaximum = 0
#========================================================================
def finmonthread(self, msg):
"""est lancé quand le thread se termine (normalement ou pas)"""
QtGui.QMessageBox.information(self,
u"Téléchargement",
msg)
#========================================================================
def stop_m(self):
"""demande l'arrêt du téléchargement avant la fin"""
if self.monthread!=None and self.monthread.isRunning():
self.monthread.stoptelech()
#========================================================================
def closeEvent(self, event):
self.stop_m()
event.accept()
#############################################################################
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
fen = Fenetre()
fen.show()
sys.exit(app.exec_()) |
Partager