1. 程式人生 > >pyqt5 在別的視窗彈出進度條

pyqt5 在別的視窗彈出進度條

要求:在匯入視訊的同時,利用caffe訓練好的模型提取視訊的特徵,這個過程比較費時間,因此需要進度條,不然以為程式死掉了。

在條用進度條出現的問題有:

1、進度條視窗可以彈出但是沒有進度條、label、button等

2、進度條視窗內容完整,但是進度條的進度沒有更新

3、進度條以上問題解決了,但在進度條視窗close()後,程式出現未響應現象。

問題一:

區分show, exec_區別

問題二:

Thread.msleep(100),模擬100個檔案

 問題三:某個迴圈出了問題,while......

進度條對話方塊:

 # -*- coding: utf-8 -*-
##set progressbar

from PyQt5.QtWidgets import QApplication,QWidget,QDialog,QLabel,QLineEdit,QProgressBar,\
    QPushButton,QVBoxLayout,QHBoxLayout,QGridLayout,QDialogButtonBox
from PyQt5.QtCore import Qt, QBasicTimer, QThread
import sys

class ProgressBar(QDialog):
    def __init__(self, fileIndex,filenum,parent = None):
        super(ProgressBar, self).__init__(parent)

        self.resize(350,100)
        self.setWindowTitle(self.tr("Processing progress"))

        self.TipLabel = QLabel(self.tr("Processing:" + "   " + str(fileIndex) + "/" + str(filenum)))
        self.FeatLabel = QLabel(self.tr("Extract feature:"))
        
        self.FeatProgressBar = QProgressBar(self)
        self.FeatProgressBar.setMinimum(0)
        self.FeatProgressBar.setMaximum(100) #總程序換算為100
        self.FeatProgressBar.setValue(0) #進度條初始值為0

        TipLayout = QHBoxLayout()
        TipLayout.addWidget(self.TipLabel)

        FeatLayout = QHBoxLayout()
        FeatLayout.addWidget(self.FeatLabel)
        FeatLayout.addWidget(self.FeatProgressBar)

        # self.startButton = QPushButton('start',self)
        self.cancelButton = QPushButton('cancel', self)
        # self.cancelButton.setFocusPolicy(Qt.NoFocus)

        buttonlayout = QHBoxLayout()
        buttonlayout.addStretch(1)
        buttonlayout.addWidget(self.cancelButton)
        # buttonlayout.addStretch(1)
        # buttonlayout.addWidget(self.startButton)

        layout = QVBoxLayout()
        # layout = QGridLayout()
        layout.addLayout(FeatLayout)
        layout.addLayout(TipLayout)
        layout.addLayout(buttonlayout)
        self.setLayout(layout)
        self.show()

        # self.startButton.clicked.connect(self.setValue)

        self.cancelButton.clicked.connect(self.onCancel)
        # self.startButton.clicked.connect(self.onStart)
        # self.timer = QBasicTimer()
        # self.step = 0

    def setValue(self,value):
        self.FeatProgressBar.setValue(value) 

    def onCancel(self,event):
        self.close()

def main():
    app = QApplication(sys.argv)
    fileIndex = '3'   #當前正在處理第幾個檔案
    filenum = '10'    #檔案總數,在label中顯示
    progress = ProgressBar(fileIndex,filenum,0)
    progress.show()
    app.exec_()

if __name__ == '__main__':
    main()

在程式中彈出對對話方塊:

self.ProgressBar = ProgressDialog.ProgressBar(self.FileIndex,self.VideoNum)
for i in range(n*step,(n+1)*step):
        # time.sleep(0.05)
        self.ProgressBar.setValue(i+1) #更新進度條的值
        QThread.msleep(int(self.ratio*100)) #模擬檔案傳送,進度條可以一點點增加,而不是一下增加很多,也可以不需要
        QApplication.processEvents()  #實時顯示
self.ProgressBar.close() #記得關閉進度條