• QtGui.QProgressBar


    A progress bar is a widget that is used when we process lengthy tasks. It is animated so that the user knows that the task is progressing. The QtGui.QProgressBar widget provides a horizontal or vertical progress bar in PyQt4 toolkit. The programmer can set the minimum and maximum value for the progress bar. The default values are 0 and 99.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    """
    ZetCode PyQt4 tutorial 
    
    This example shows a QtGui.QProgressBar widget.
    
    author: Jan Bodnar
    website: zetcode.com 
    last edited: September 2011
    """
    
    import sys
    from PyQt4 import QtGui, QtCore
    
    class Example(QtGui.QWidget):
        
        def __init__(self):
            super(Example, self).__init__()
            
            self.initUI()
            
        def initUI(self):      
    
            self.pbar = QtGui.QProgressBar(self)
            self.pbar.setGeometry(30, 40, 200, 25)
    
            self.btn = QtGui.QPushButton('Start', self)
            self.btn.move(40, 80)
            self.btn.clicked.connect(self.doAction)
    
            self.timer = QtCore.QBasicTimer()
            self.step = 0
            
            self.setGeometry(300, 300, 280, 170)
            self.setWindowTitle('QtGui.QProgressBar')
            self.show()
            
        def timerEvent(self, e):
          
            if self.step >= 100:
            
                self.timer.stop()
                self.btn.setText('Finished')
                return
                
            self.step = self.step + 1
            self.pbar.setValue(self.step)
    
        def doAction(self):
          
            if self.timer.isActive():
                self.timer.stop()
                self.btn.setText('Start')
                
            else:
                self.timer.start(100, self)
                self.btn.setText('Stop')
            
    def main():
        
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()    
    

    In our example we have a horizontal progress bar and a push button. The push button starts and stops the progress bar.

    self.pbar = QtGui.QProgressBar(self)
    

    This is a QtGui.QProgressBar constructor.

    self.timer = QtCore.QBasicTimer()
    

    To activate the progress bar, we use a timer object.

    self.timer.start(100, self)
    

    To launch a timer event, we call its start() method. This method has two parameters: the timeout and the object which will receive the events.

    def timerEvent(self, e):
      
        if self.step >= 100:
        
            self.timer.stop()
            self.btn.setText('Finished')
            return
            
        self.step = self.step + 1
        self.pbar.setValue(self.step)
    

    Each QtCore.QObject and its descendants have a timerEvent() event handler. In order to react to timer events, we reimplement the event handler.

    def doAction(self):
      
        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText('Start')
            
        else:
            self.timer.start(100, self)
            self.btn.setText('Stop')
    

    Inside the doAction() method, we start and stop the timer.

    QtGui.QProgressBarFigure: QtGui.QProgressBar

  • 相关阅读:
    SharePoint 2007 开发部署Application Pages
    SharePoint 列表项通过自定义WebService读取
    SharePoint 用户控件编写的简单介绍
    SharePoint 读取选项字段所有Choise
    SharePoint WebService 之更新审批状态
    ASP.NET TextBox TextMode为Password从数据库绑定不能显示问题
    String.prototype.split()兼容问题
    使用SQLBULKCOPY提高导入数据的性能
    关于SQL SERVER2005附加数据库显示为只读的解决方案
    从客户端(hd_content="<DIV>测试昂,哈333</DIV>")中检测到有潜在危险的 Request.Form 值。
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4435757.html
Copyright © 2020-2023  润新知