我的python QT代码有什么问题

问题描述 投票:2回答:2

这是我用python QT编写的下载代码。并继续显示以下错误:

'C:\Python34\python.exe "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py" --multiproc --client 127.0.0.1 --port 52063 --file C:/Users/bablu/PycharmProjects/pydownloader/structure.py
pydev debugger: process 4108 is connecting

Connected to pydev debugger (build 135.1057)

Process finished with exit code -1'

我的代码:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import urllib.request

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.url.setPlaceholderText("URL")
        self.save_location.setPlaceholderText("File Save Location")


        self.progress.setValue(0)
        self.progress.setAlignment(Qt.AlignHCenter)


        layout.addWidget(self.url)
        layout.addWidget(self.save_location)
        layout.addWidget(self.progress)
        layout.addWidget(download)

        self.setLayout(layout)

        self.setWindowTitle("python downloader")
        self.setFocus()

        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)

        QMessageBox.information(self, "Information", "download complete")

    def report(self, blocknum, blocksize, totalsize):
        readsofar = blocknum * blocksize

        if totalsize > 0:
            percent = readsofar * 100/totalsize
            self.progress.setValue(int(percent))


app = QApplication(sys.argv)
dl = Downloader()
dl.show()
app.exec_()
python qt4 pycharm
2个回答
0
投票

尝试导入并使用PyQt5代替。这对我有用。

from PyQt5.QtCore import *
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLineEdit, QProgressBar, QPushButton, QMessageBox, QApplication

import sys
import urllib.request

0
投票

尝试使用文件名而不是文件夹路径,例如“file.ext”

© www.soinside.com 2019 - 2024. All rights reserved.