Skip to content

Instantly share code, notes, and snippets.

@delirious-lettuce
Created May 30, 2017 04:29
Show Gist options
  • Save delirious-lettuce/324fdb51f1fdd0cfd76bfd257ea17e05 to your computer and use it in GitHub Desktop.
Save delirious-lettuce/324fdb51f1fdd0cfd76bfd257ea17e05 to your computer and use it in GitHub Desktop.
import sys
import urllib.request
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Downloader(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
self.url = QLineEdit()
self.save_location = QLineEdit()
self.progress = QProgressBar()
download = QPushButton("Download")
self.progress.setValue(0)
self.progress.setAlignment(Qt.AlignHCenter)
self.url.setPlaceholderText("URL")
self.save_location.setPlaceholderText("Save folder:")
layout.addWidget(self.url)
layout.addWidget(self.save_location)
layout.addWidget(self.progress)
layout.addWidget(download)
self.setLayout(layout)
self.setWindowTitle("Downloader")
download.clicked.connect(self.download)
def download(self):
url = self.url.text()
save_location = self.save_location.text()
urllib.request.urlretrieve(url, save_location, self.report)
try:
urllib.request.urlretrieve(url, save_location, self.report)
except Exception:
QMessageBox.warning(self, "Warning", "The download failed")
return
QMessageBox.information(self, "Information", "Download is complete")
self.progress.setValue(0)
self.url.setText("")
self.save_location.setText("")
def report(self, blocknum, blocksize, totalsize):
readsofar = blocknum * blocksize
if totalsize > 0:
percent = readsofar * 100 / totalsize
self.progress.setValue(int(percent))
if __name__ == '__main__':
app = QApplication(sys.argv)
dl = Downloader()
dl.show()
app.exec_()
sys.exit(app.exec_())
@XBlack97
Copy link

XBlack97 commented Apr 20, 2018

Didn't work for me...
1
2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment