Continuing with the series of articles about PyQt, this one covers the progress bar widget.

The following widgets will be used:

  • Qbutton: Button to start and stop the progress bar, and the quit button.
  • QProgressBar (broken link): The progress bar widget.
  • QTimer (broken link): Provides a timer that drives the progress of the bar.

The progress bar starts when the button is clicked and increases until it reaches 100. The button that starts the bar also serves to stop it and to resume the progress; when it reaches 100 the button is set back to start the bar, and the counter is reset to zero so the increment can start from zero again. There is a button to quit the application.

The program code is shown below:

#!/usr/bin/env python

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore


class App(QtGui.QWidget):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self, parent)
        #Se define el tamano de la ventana
        self.setGeometry(400, 400, 250, 150)
        #Se le coloca un titulo a la ventana y se asocia un icono.
        self.setWindowTitle('Barra de progreso')
        self.setWindowIcon(QtGui.QIcon('./openlogo-50.png'))

        #Se definen los widgets a utilizar en la ventana.
        #Se crea la instancia de la barra de progreso.
        self.pbar = QtGui.QProgressBar(self)
        #Se crea el boton que inicia/detiene la barra de progreso
        self.button = QtGui.QPushButton('Iniciar', self)
        #Se define una politica al boton de no foco
        self.button.setFocusPolicy(QtCore.Qt.NoFocus)
        #Se define el boton salir.
        self.quit = QtGui.QPushButton('Salir', self)

        #Se crea la instancia timer con el contador en cero
        self.timer = QtCore.QBasicTimer()
        self.step = 0

        #se asocia los eventos de los 2 botones. 
        self.connect(self.button, QtCore.SIGNAL('clicked()'),self.Accion)
        self.connect(self.quit, QtCore.SIGNAL('clicked()'),QtGui.qApp, QtCore.SLOT('quit()'))

        #Se define como empaquetar los widgets.
        #En este caso se usa grilla.
        #Se crea la instancia
        grid = QtGui.QGridLayout()
        grid.setSpacing(10)
        #Se agrupan los widgets pasando el
        #objeto luego el orden en vertical, el orden en
        #horizontal.
        grid.addWidget(self.pbar, 1, 0)
        grid.addWidget(self.button, 2, 0)
        grid.addWidget(self.quit, 3, 1)
        #Se define el layout pasando la grilla
        self.setLayout(grid)



    def timerEvent(self, event):
        #funcion asociada al timer.
        #Si el contador llega a 100
        #Se detiene el timer, se cambia el titulo
        #del boton, se coloca el contador en cero
        #y se sale de la funcion.
        #Si no llega a 100, se incrementa en 1 el contador y
        #se le asigna un nuevo valor a la barra de progreso.
        if self.step >= 100:
            self.timer.stop()
            self.button.setText('Iniciar')
            self.step = 0
            return
        self.step = self.step + 1
        self.pbar.setValue(self.step)

    def Accion(self):
        #Si el timer esta activo se detiene y se
        #le cambia el titulo al boton con Iniciar.
        if self.timer.isActive():
            self.timer.stop()
            self.button.setText('Iniciar')
        else:
            #Si no esta activo el timer, se inicia con valor de 100
            #se coloca el titulo detener al boton.
            self.timer.start(100, self)
            self.button.setText('Detener')

#Se ejecuta el programa principal
if __name__ == "__main__":    
   #Se instancia la clase QApplication    
   app = QtGui.QApplication(sys.argv)    
   #Se instancia el objeto QuitButton    
   qb = App()    
   #Se muestra la aplicacion    
   qb.show()    
   #Se sale de la aplicacion    
   sys.exit(app.exec_())

===

Make a donation! If you liked the article you can make a donation with Bitcoin (BTC) using the digital wallet of your choice to the following address: 17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV

Or by scanning the QR code from your wallet:

17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV *