pyqt5 实现在别的窗口弹出进度条

yipeiwu_com4年前Python基础

要求:在导入视频的同时,利用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() #记得关闭进度条

以上这篇pyqt5 实现在别的窗口弹出进度条就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python解决汉字编码问题:Unicode Decode Error

前言 最近由于项目需要,需要读取一个含有中文的txt文档,完了还要保存文件。文档之前是由base64编码,导致所有汉字读取显示乱码。项目组把base64废弃之后,先后出现两个错误:...

Python中调用其他程序的方式详解

Python中调用其他程序的方式详解

前言 在Python中,可以方便地使用os模块来运行其他脚本或者程序,这样就可以在脚本中直接使用其他脚本或程序提供的功能,而不必再次编写实现该功能的代码。为了更好地控制运行的进程, 可...

Python 如何优雅的将数字转化为时间格式的方法

将数字转化成时间格式 from dateutil.parser import parse a=20170825 b=str(a) c=parse(b) print(c) 20...

python opencv读mp4视频的实例

如下所示: #获得视频的格式 videoCapture = cv2.VideoCapture('/home/lw/3661.mp4') #获得码率及尺寸 fps = videoC...

理解python多线程(python多线程简明教程)

对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂。所以,这里力图用简单的例子,让你对多线程有个初步的认识。 单线程   在好些年前的MS-DOS时代,操作系...