multithreading - How to see a file download progress without the GUI freezing (python 3.4, pyQt5) using QThread -
multithreading - How to see a file download progress without the GUI freezing (python 3.4, pyQt5) using QThread -
so i'm trying create simple file downloader in python 3.4.2 , pyqt5
qthreads seems way there's no tutorials online or examples understand pyqt5. find qt5 reference c/c++ , bunch of pyqt4 tutorials don't work pyqt5 , python 3
here's gui screenshot: http://i.imgur.com/kgjqrrk.png
and here's code:
#!usr/bin/env python3 pyqt5.qtcore import * pyqt5.qtwidgets import * string import template import urllib.request import sys class form(qwidget): def __init__(self, parent=none): super(form, self).__init__(parent) lblurl= qlabel("file url:") self.txturl = qlineedit() self.bttdl = qpushbutton("&download") self.pbar = qprogressbar() self.pbar.setminimum(0) buttonlayout1 = qvboxlayout() buttonlayout1.addwidget(lblurl) buttonlayout1.addwidget(self.txturl) buttonlayout1.addwidget(self.bttdl) buttonlayout1.addwidget(self.pbar) self.bttdl.clicked.connect(self.bttpush) mainlayout = qgridlayout() mainlayout.addlayout(buttonlayout1, 0, 1) self.setlayout(mainlayout) self.setwindowtitle("pysfd") def bttpush(self): # check if download running or disable button # while it's running url = self.txturl.text() if url == "": qmessagebox.information(self, "empty url", "please come in url of file want download.") homecoming else: filename = str(qfiledialog.getsavefilename(self, 'choose download location , file name', '.')) filename = filename[:-6] filename = filename.split("('",maxsplit=1)[1] self.dlthread = downloaderthread() self.dlthread.connect(dlthread.run) self.dlthread.start() self.dlthread.emit(url) class downloaderthread(qthread): def __init__(self): qthread.__init__(self) def __del__(self): self.wait() def run(self, dllink): while dllink.length == 0: qthread.sleep(1) pass def report(block_count, block_size, total_size): if block_count == 0: self.pbar.setvalue(0) if (block_count * block_size) == total_size: qmessagebox.information(self,"done!","download finished!") homecoming self.pbar.setvalue(self.pbar.value() + block_size) urllib.request.urlretrieve(dllink, filename, reporthook=report) self.terminate() if __name__ == '__main__': app = qapplication(sys.argv) screen = form() screen.show() sys.exit(app.exec_())
i've tried lot of ways , doesn't seem work.
how create progress bar (self.pbar
) show download progress in real time?
ps. downloader on github: https://github.com/dkatara/pysfd
fixed it, it's not done yet, threading works great!
most recent version on github here: https://github.com/dkatara/pysfd
#!usr/bin/env python3 pyqt5.qtcore import * pyqt5.qtwidgets import * import urllib.request import sys import threading dlthread = 0 hwindow = 0 fprogresscounter = 0.0 class form(qwidget): def __init__(self, parent=none): super(form, self).__init__(parent) global hwindow hwindow = self lblurl = qlabel("file url:") self.txturl = qlineedit() self.bttdl = qpushbutton("&download") self.pbar = qprogressbar() self.pbar.setminimum(0) self.pbar.setmaximum(100) buttonlayout1 = qvboxlayout() buttonlayout1.addwidget(lblurl) buttonlayout1.addwidget(self.txturl) buttonlayout1.addwidget(self.bttdl) buttonlayout1.addwidget(self.pbar) self.bttdl.clicked.connect(self.bttpush) mainlayout = qgridlayout() mainlayout.addlayout(buttonlayout1, 0, 1) self.setlayout(mainlayout) self.setwindowtitle("pysfd") def bttpush(self): global dlthread hsignals = sighandling() hsignals.dlprogress_update.connect(hsignals.pbar_incrementer) hsignals.dlprogress_done.connect(hsignals.dldone) url = self.txturl.text() if url == "": qmessagebox.information(self, "empty url", "please come in url of file want download.") homecoming else: filename = str(qfiledialog.getsavefilename(self, 'choose download location , file name', '.')) ## observe cancel filename = filename[:-6] filename = filename.split("('",maxsplit=1)[1] self.bttdl.setenabled(false) dlthread = threading.thread(target=hsignals.rundl,args=(url, filename)) dlthread.start() homecoming def pbarincvalue(self, val): global fprogresscounter #print("pbarincvalue({0})\nfprogresscounter={1}".format(val,fprogresscounter)) if self.pbar.value() >= 100: self.dlprogress_done.emit() homecoming if fprogresscounter > 1.0: # prepare self.pbar.setvalue(self.pbar.value() + 1) fprogresscounter -= 1.0 fprogresscounter += val else: fprogresscounter += val class sighandling(qobject): dlprogress_update = pyqtsignal(float) dlprogress_done = pyqtsignal() @pyqtslot(float) def pbar_incrementer(self, val): hwindow.pbarincvalue(val) @pyqtslot() def dldone(self): print("in dldone") hwindow.pbar.setvalue(100) hwindow.bttdl.setenabled(true) def rundl(self, dllink, filename): #print("in run") global dlthread, hwindow def report(block_count, block_size, total_size): if block_count == 0: #print("block_count == 0") self.dlprogress_update.emit(0) if (block_count * block_size) == total_size: self.dlprogress_done.emit() incamount = float((100*block_size) / total_size) #print("bs={0} ts={1} incamount={2}".format(block_size,total_size,incamount)) self.dlprogress_update.emit(incamount) urllib.request.urlretrieve(dllink, filename, reporthook=report) #print("emit dlprogress_done") self.dlprogress_done.emit() #print("about leave dlthread") pass if __name__ == '__main__': app = qapplication(sys.argv) screen = form() screen.show() sys.exit(app.exec_())
python multithreading python-3.x pyqt pyqt5
Comments
Post a Comment