【发布时间】:2014-12-18 16:44:19
【问题描述】:
所以我正在尝试在 Python 3.4.2 和 PyQt5 中制作一个简单的文件下载器
QThreads 似乎是这样,但没有在线教程或我可以理解的 PyQt5 示例。我能找到的只是 C/C++ 的 Qt5 参考和一堆不适用于 PyQt5 和 Python 3 的 PyQt4 教程
这是 GUI 屏幕截图:http://i.imgur.com/KGjqRRK.png
这是我的代码:
#!usr/bin/env python3
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from 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 the download is already running or just disable the button
# while it's running
url = self.txtURL.text()
if url == "":
QMessageBox.information(self, "Empty URL",
"Please enter the URL of the file you want to download.")
return
else:
filename = str(QFileDialog.getSaveFileName(self, 'Choose the download location and 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!")
return
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_())
我尝试了很多方法,但似乎都不起作用。
如何让进度条(self.pbar)实时显示下载进度?
PS。这个下载器在我的 GitHub 上:https://github.com/dKatara/pySFD
【问题讨论】:
-
你有两个选择:使用 QThread 和使用信号/插槽让线程说话;或使用 python 线程并使用队列进行交谈。 stackoverflow.com/questions/26472069/…后者的一个例子
标签: python multithreading python-3.x pyqt pyqt5