QProgressBar不显示复制进度

时间:2019-02-27 02:16:26

标签: python python-3.x pyqt pyqt5

我正在创建一个显示复制文件进度的应用程序,复制过程运行良好,问题是QProgressBar未更新为适当的值

copy ()类的Copy Thread函数负责实现将Src_School_Admin文件夹及其中的文件复制到在此情况下创建的新目录{{ 1}}文件夹。

要更新SchoolAdministration的值,我执行了一个循环,一旦目标目录QProgressBarSchoolAdministration的权重相同,就更新目标目录的大小值,然后循环坏了。

Src_School_Admin信号接收一个用于计算百分比的操作作为参数self.procPartDone

但是,size_original_folder * 100 / target_folder_size的值未更新,希望您能为我提供帮助

File.py

QProgressBar

Dialog_Install.ui

from PyQt5.QtWidgets import QMainWindow,QApplication,QMessageBox
from PyQt5 import uic,QtCore
import shutil,os
import platform
import threading

class CopyThread(QtCore.QThread):
    procDone = QtCore.pyqtSignal(bool)
    procPartDone = QtCore.pyqtSignal(int)

    def __init__(self,origen,destino,tamaño_origen):
        QtCore.QThread.__init__(self)
        self.origen = origen
        self.destino = destino
        self.tamaño = tamaño_origen

    def run(self):
        self.copy()
        self.procDone.emit(True)

    def copy(self):
        print("copi hilo")
        source_destino = 'C:/Program Files/SchoolAdministration'
        self.total_destino = 0

        while self.tamaño > self.total_destino:
            for source,dirs,files in os.walk(source_destino):
                for f in files:
                    fp = os.path.join(source,f)
                    self.total_destino+= os.path.getsize(fp)
                    print(self.total_destino,self.tamaño)
                    self.procPartDone.emit(self.total_destino*100/self.tamaño)



class MessageError(threading.Thread):
    def __init__(self,obj):
        threading.Thread.__init__(self)
        self.obj = obj

    def run(self):
        QtCore.QMetaObject.invokeMethod(self.obj,"onError",
            QtCore.Qt.QueuedConnection,
            QtCore.Q_ARG(str,"Critical Access"),
            QtCore.Q_ARG(str,"\nEste programa solo puede ejecutarse con permisos de Administrador.\n\nInfo:\nAsegurese de iniciar el programa con permisos de Administrador\n"))

class Install(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("Dialog_Install.ui",self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        self.BClose.clicked.connect(self.Info)
        self.BIniciar.pressed.connect(self.Button_Effect_start)
        self.BIniciar.released.connect(self.Button_Effect_End)
        self.progressBar.hide()


    def Info(self):
        respuesta  = QMessageBox.question(None,"Salir","Desea finalizar la intalación del programa\t")
        if respuesta == QMessageBox.Yes:
            self.close()

    def Button_Effect_start(self):
        animacion = QtCore.QPropertyAnimation(self.BIniciar, b"size")
        animacion.setDuration(100)
        animacion.setStartValue(QtCore.QSize(138,28))
        animacion.setEndValue(QtCore.QSize(138,28))
        animacion.start()

    def Button_Effect_End(self):
        animacion = QtCore.QPropertyAnimation(self.BIniciar, b"size")
        animacion.setDuration(100)
        animacion.setStartValue(QtCore.QSize(141,31))
        animacion.setEndValue(QtCore.QSize(141,31))
        animacion.start()
        self.BIniciar.hide()
        self.progressBar.show()
        self.LEstado.setText("Copiando archivos " + str(self.progressBar.value()) + "%")
        self.label_5.setText("Instalando")
        self.LEstado.setText("Creando directorio")
        t=threading.Thread(target=self.Ubicacion)
        t.start()

    def Ubicacion(self):

        arquitectura = platform.architecture()

        target = "Src_School_Admin"
        initial_dir = 'C:\\'
        path = ''

        for root, _,files in os.walk(initial_dir):
            if target in root:
                path = os.path.join(root)
                break


        tamaño_origen = 0
        start = "."
        source = path.replace("\\","/")

        for source,dirs,files in os.walk(source):
            for f in files:
                fp = os.path.join(source,f)
                tamaño_origen += os.path.getsize(fp)

        if os.path.exists('C:\\Program Files\\SchoolAdministration'):
            shutil.rmtree('C:\\Program Files\\SchoolAdministration')
            self.copy1(path,tamaño_origen)

        else:

            self.copy1(path,tamaño_origen)



    def copy1(self,path,tamaño_origen):
        origen = path.replace("\\",'/')
        print(origen)
        destino = 'C:\\Program Files\\SchoolAdministration'
        shutil.copytree(origen, destino)
        self.copy_thread = CopyThread(origen, destino,tamaño_origen)
        self.copy_thread.procPartDone.connect(self.update_progress)
        self.copy_thread.procDone.connect(self.finished_copy)
        self.copy_thread.start()

    def update_progress(self,progress):
        print(progress,"progress")
        self.progressBar.setValue(progress)
    def finished_copy(self,state):
        self.close()


    @QtCore.pyqtSlot(str,str)
    def onError(self,title,text):
        QMessageBox.critical(None,title,text)
        self.close()

app = QApplication([])
i = Install()
i.show()
app.exec_()

1 个答案:

答案 0 :(得分:3)

我尝试了您的示例,一切都很好,QProgressBar的更新非常快:),所以​​我放慢了速度,QtCore.QThread.msleep (100)

# ...
def copy(self):
    print("copi hilo-> {}({}) -> {}".format(self.origen, self.tamaño, self.destino))
    source_destino = 'C:/Program Files/SchoolAdministration'
    #source_destino = 'D:\\Test\\SchoolAdministration'
    self.total_destino = 0

    while self.tamaño > self.total_destino:
        for source, dirs, files in os.walk(source_destino):
            for f in files:
                fp = os.path.join(source, f)
                self.total_destino += os.path.getsize(fp)
                print("\nemit-> {} = {} * 100 / {} "
                      "".format(self.total_destino*100//self.tamaño,     # //
                                self.total_destino, 
                                self.tamaño))
                print("f=`{}` --> fp=`{}`"
                      "".format( f, fp))

                self.procPartDone.emit(self.total_destino*100/self.tamaño)

                QtCore.QThread.msleep(100)                               # <---
# ...

enter image description here

相关问题