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() #记得关闭进度条

当程序在执行时,我们可以使用PyQt库中的QProgressBar类来实现弹出进度条。下面是一个简单的示例代码: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QProgressBar from PyQt5.QtCore import Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() self.progress_bar = QProgressBar(self) # 创建一个进度条对象 self.progress_bar.setGeometry(30, 40, 200, 25) # 设置进度条的位置和大小 self.setWindowTitle("进度条示例") self.setGeometry(300, 300, 280, 170) self.timer = QTimer() # 创建一个定时器 self.timer.timeout.connect(self.update_progress) # 连接定时器的timeout信号和更新进度的槽函数 self.show() def update_progress(self): current_value = self.progress_bar.value() # 获取当前进度条的值 if current_value < 100: self.progress_bar.setValue(current_value + 1) else: self.timer.stop() # 进度已经达到100,停止定时器 def start_progress(self): self.progress_bar.setValue(0) # 将进度条的值设置为0 self.timer.start(100) # 每隔100毫秒触发一次timeout信号,来更新进度条的值 if __name__ == "__main__": app = QApplication([]) window = MainWindow() window.start_progress() app.exec_() ``` 在这个示例中,我们创建了一个名为MainWindow的窗口类,其中包含一个QProgressBar类的实例对象progress_bar,用于显示进度条。在start_progress方法中,我们首先将进度条的值设置为0,然后使用定时器timer的start方法来启动定时器,并指定定时器每隔100毫秒触发一次timeout信号。在update_progress方法中,我们通过调用进度条的setValue方法将进度条的值更新为当前值+1,实现进度条的增加效果。当进度条的值达到100时,我们停止定时器的运行。 最后,我们创建了一个应用程序QApplication的实例app,并创建了MainWindow的实例对象window,并调用它的start_progress方法来开始显示进度条。最后,我们通过调用app的exec_方法来运行应用程序,并保持其处于运行状态。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值