脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pyqt5 实现在别的窗口弹出进度条

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

2021-07-14 11:00竹子熊猫 Python

今天小编就为大家分享一篇pyqt5 实现在别的窗口弹出进度条,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

要求:在导入视频的同时,利用caffe训练好的模型提取视频的特征,这个过程比较费时间,因此需要进度条,不然以为程序死掉了。

在条用进度条出现的问题有:

1、进度条窗口可以弹出但是没有进度条、label、button等

2、进度条窗口内容完整,但是进度条的进度没有更新

3、进度条以上问题解决了,但在进度条窗口close()后,程序出现未响应现象。

问题一:

区分show, exec_区别

问题二:

thread.msleep(100),模拟100个文件

问题三:某个循环出了问题,while......

进度条对话框:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- 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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    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()

在程序中弹出对对话框:

?
1
self.progressbar = progressdialog.progressbar(self.fileindex,self.videonum)
?
1
2
3
4
5
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() #实时显示
?
1
self.progressbar.close() #记得关闭进度条

以上这篇pyqt5 实现在别的窗口弹出进度条就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/summermaoz/article/details/6058084

延伸 · 阅读

精彩推荐