Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python PyQt4 Downloader with progressbar
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from urllib2 import *
class DownloaderApp(QtGui.QMainWindow):
def __init__(self, parent = None):
super(DownloaderApp, self).__init__(parent)
self.setObjectName("download")
self.groupBox = QtGui.QGridLayout()
self.groupBox.setObjectName("groupBox")
self.resize(500, 150)
self.progressBar = QtGui.QProgressBar()
self.progressBar.setLayoutDirection(QtCore.Qt.LeftToRight)
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.progressBar.setVisible(False)
self.progressBar.setStyleSheet(stylesheet(self))
self.plainTextEdit = QtGui.QLineEdit()
self.plainTextEdit.setObjectName("plainTextEdit")
self.plainTextEdit.text()==''
self.pushButton = QtGui.QPushButton()
self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton.setObjectName("pushButton")
self.pushButton.setFixedWidth(130)
self.pushButton.setText("start Download")
self.label = QtGui.QLabel()
self.label.setObjectName("label")
self.label.setText("URL:")
self.statuslabel = QtGui.QLabel()
self.statuslabel.setText("status")
self.groupBox.addWidget(self.label, 0, 0 )
self.groupBox.addWidget(self.plainTextEdit, 1, 0 , 1, 1)
self.groupBox.addWidget(self.pushButton, 2, 0)
self.groupBox.addWidget(self.progressBar, 3, 0, 1, 2)
self.groupBox.addWidget(self.statuslabel, 4, 0, 1, 1 , Qt.AlignBottom)
qw = QtGui.QWidget()
qw.setLayout(self.groupBox)
self.setCentralWidget(qw)
self.setWindowTitle("Downloader")
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.startDownload)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.plainTextEdit.clear)
QtCore.QMetaObject.connectSlotsByName(self)
def startDownload(self):
if not self.plainTextEdit.text() or self.plainTextEdit.text()=='':
QMessageBox.information(myDownloader,"Warning","no link")
return
self.progressBar.setVisible(True)
QtGui.QApplication.processEvents(QtCore.QEventLoop.AllEvents)
enter=self.plainTextEdit.text()
url=urlopen(('%s')%(enter))
html=url.info()
name = enter.split('/')[-1]
name = QDir.homePath() + "/Downloads/" + name
cl=html['Content-Length']
f =open(name,'wb+')
fl=0
self.fl=0
while 1:
QtGui.QApplication.processEvents(QtCore.QEventLoop.AllEvents)
xr=url.read(1024)
fl+=len(xr)
self.progressBar.setValue(fl*100/int(cl))
QtGui.qApp.processEvents()
f.write(xr)
if not xr:break
del xr
QtGui.QApplication.processEvents(QtCore.QEventLoop.AllEvents)
f.close()
QMessageBox.information(myDownloader,"Message","Download complete")
self.progressBar.setVisible(False)
return
def stylesheet(self):
return """
QProgressBar {
border: 2px solid #D9D9D9;
border-radius: 5px;
text-align: center;
}
QProgressBar::chunk {
background-color: #05B8CC;
width: 20px;
}
"""
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
myDownloader = DownloaderApp()
myDownloader.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.