我有pyqt5和urllib和线程的进度栏的问题

时间:2019-07-06 20:17:01

标签: python multithreading pyqt5 urllib

我在线程和进度栏和Urllib上遇到问题,我想让应用程序从网络上下载,这个问题使我烦恼,我不知道如何处理它,我发现它使Qt4而不是Qt5成为线程。并且我尝试使用以下方法:QApplication.ProcessEvents()使应用程序运行缓慢,这并不是我搜索的最佳方法,我通过创建另一个线程找到了方法

#!/usr/bin/python3

# import Important modules
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType

from os import path
import sys
import urllib.request
import os 
import threading
import time
import pafy

# import UI file
FORM_CLASS,_ = loadUiType(path.join(path.dirname(__file__),'Downlod.ui'))

class mainApp(QMainWindow, FORM_CLASS):
    def __init__(self, parent=None):
        super(mainApp,self).__init__(parent)
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.Handel_Ui()
        self.Handel_Button()

    def Handel_Ui(self):
        # To Center Window In Screen
        qtRectangle = self.frameGeometry()
        centerPoint = QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)
        self.move(qtRectangle.topLeft())
        qtRectangle = self.frameGeometry()
        centerPoint = QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)
        self.move(qtRectangle.topLeft())
        self.setWindowTitle("Downloader App")
        self.tabWidget.tabBar().setVisible(False)
        self.progressBar.setValue(0)


        ######Button#########
    def Handel_Button(self):
        self.pushButton.clicked.connect(self.Main_App)
        self.pushButton_2.clicked.connect(self.Signle_Download)
        self.pushButton_3.clicked.connect(self.Youtube_Download)
        self.pushButton_4.clicked.connect(self.Settings_App)
        self.pushButton_16.clicked.connect(self.run_thread)
        self.pushButton_5.clicked.connect(self.runThread.Handel_Browse)



    #########Handel Button On TabWidget #########
    def Main_App(self):
        self.tabWidget.setCurrentIndex(0)

    def Signle_Download(self):
        self.tabWidget.setCurrentIndex(1)

    def Youtube_Download(self):
        self.tabWidget.setCurrentIndex(2)


    def Settings_App(self):
        self.tabWidget.setCurrentIndex(3)

    ##############################################
    ################Singl Download################
    def start_progressbar(self):
        self.run_thread = runThread(parent=None)
        self.run_thread.start()
        self.run_thread.connect(self.progressBar.setValue(counter_value))



class runThread(QThread):
    counter_value = pyqtSignal(int)
    def __init__(self,parent=None):
        supe(runThread,self).__init__(parent)


    def run (self):
        url= self.lineEdit.text()
        save= self.lineEdit_2.text()
        try:
            d=urllib.request.urlretrieve(url,save,self.Handel_ProgressBar)
        except Exception as f:
            QMessageBox.warning(self,"Download Error" ,'The issue is > {}'.format(f))
        QMessageBox.information(self,"Download Completed",'The Download Finshed')        
        self.progressBar.setValue(0)
        self.lineEdit.setText('')
        self.lineEdit_2.setText('')



    def Handel_ProgressBar(self,block_num,blocksize,totalsize):
        if totalsize>0:
            downloaded = block_num*blocksize
            percent=(downloaded*100)/totalsize
            self.progressBar.setValue(counter_value)
            self.counter_value.emit()


    def Handel_Browse(self):
        save_file = QFileDialog.getSaveFileName(self,caption='Save As',directory = '.',filter='Allfiles (*.*)')
        text=(save_file[0])
        self.lineEdit_2.setText(text)

def main():
    app = QApplication(sys.argv)
    window = mainApp()
    window.show()
    app.exec_()

if __name__ == "__main__":
    main()

>,我正在尝试搜索,但找不到任何相关信息

问题在于:

    Traceback (most recent call last):
  File "Main_App.py", line 124, in <module>
    main()
  File "Main_App.py", line 119, in main
    window = mainApp()
  File "Main_App.py", line 26, in __init__
    self.Handel_Button()
  File "Main_App.py", line 56, in Handel_Button
    self.pushButton_16.clicked.connect(self.run_thread)
AttributeError: 'mainApp' object has no attribute 'run_thread'

0 个答案:

没有答案