Status bar

A status bar is a horizontal information area usually found at the bottom of windows in a GUI. Its primary job is to display information about the current status of the window. A status bar can also be divided into sections, each showing different information to the users.

In PySide, a status bar can be added to the QMainWindow class by calling the function QMainWindow.setStatusBar(statusbar). It takes the object of PySide.QtGui .QStatusBar as a parameter. The properties of the status bar is defined by this class and an object of this class is returned to set a status bar. Setting this parameter to 0 will remove the status bar from the main window. A status bar can show information that can fall into any of the following three categories:

  • Temporary: Briefly occupies most of the status bar and is mainly used to explain tool tip texts, menu entries, and so on
  • Normal: Occupies a part of the status bar and may be temporarily hidden by temporary messages, and is used to display the current window information, page and line numbers and so on
  • Permanent: Usually occupies a little space and is used to indicate important mode information, Caps Lock indicator, spell check info, and so on

The current status of the status bar can be retrieved by using the QMainWindow.statusBar() function. The temporary messages to the status bar can be set by calling the QStatusBar.showMessage(text[,timeout=0]) function. If a timeout is set, the message will be cleared after the expiry of specified time in milliseconds. If there is no timeout, you can clear the message by calling the QStatusBar.clearMessage() function.

To evident this, we create a method called CreateStatusBar(), which has the following code snippet:

  def CreateStatusBar(self):
    """ Function to create Status Bar
    """
    self.myStatusBar = QStatusBar()
    self.myStatusBar.showMessage('Ready', 2000)
    self.setStatusBar(self.myStatusBar)

Now call this function, from the main application to set a status bar on the main window. On executing this code, we could see a status bar appearing on the main window that will expire after 2 seconds, which is the timeout that we have set. If you have left the timeout option or set it as 0, the message will appear in the status bar till another message is called on to overwrite it or till we close the application. The output window will be as given in the following screenshot.

It is also possible to set a widget in the status bar in addition to the text messages. A widget like QProgressBar can be added to the status bar to indicate the progress of a particular action on the main window. The PySide.Q tGui.QProgressBar widget provides a horizontal or vertical progress bar. The progress bar is used to intimate the user an indication of the progress of an operation and to reassure them that the application is still running. We shall now see a program that implements this. Make the changes to the previous program explained as follows:

Status bar

The setMinimum(minimum) and setMaximum(maximum) functions of PySide.QtGui.QProgressBar takes an integer value as the value to set the minimum and maximum possible step values and it will display the percentage of steps that have been completed when you later give it the current step value. The percentage is calculated by dividing the progress as follows:

[PySide.QtGui.QProgressBar.value() -PySide.QtGui.QProgressBar.minimum()]
-------------------------------------------------------------------------------[PySide.QtGui.QProgressBar.maximum() - PySide.QtGui.QProgressBar.
minimum()]

  def __init__(self):
...
  self.setGeometry(300, 250, 400, 300)
    self.statusLabel = QLabel('Showing Progress')
    self.progressBar = QProgressBar()
    self.progressBar.setMinimum(0)
    self.progressBar.setMaximum(100)
...

As explained, we will now add widgets to the status bar. For this purpose, we have created two widgets namely, self.statusLabel and self.progressBar of type QLabel and QProgressBar respectively. The following code creates the status bar and add these widgets to it:

  def CreateStatusBar(self):
    """ Function to create the status bar
    """
    self.myStatusBar = QStatusBar()
    self.progressBar.setValue(10)
    self.myStatusBar.addWidget(self.statusLabel, 1)
    self.myStatusBar.addWidget(self.progressBar, 2)
    self.setStatusBar(self.myStatusBar)

The function PySide.QtGui.QStatusBar.addWidget(widget[, stretch=0]) takes two arguments. The first mandatory argument is any valid QWidget object that is to be added on the status bar and the second optional parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The number defines the ratio of the status bar that the widget can use. By setting the progress bar's stretch factor to 2, we ensure that it takes two-third of the total area of the status bar.

After adding the widgets to the status bar, we write the following function to show its working state. This function will show the label as "Showing Progress" till the progress bar at the right proceeds to completion. The progress bar increments by 10 percent every time till completion after a short sleep for one second. On completion, the label changes to Ready.

  def ShowProgress(self):
   """ Function to show progress
   """
    while(self.progressBar.value() < self.progressBar.maximum()):
      self.progressBar.setValue(self.progressBar.value() + 10)
      time.sleep(1)
    self.statusLabel.setText('Ready')

Now, modify the code in the main block, as shown in the following information box and execute the program. You can now see that the status bar has a working progress bar widget along with a label.

Note

....        
        mainWindow = MainWindow()
        mainWindow.CreateStatusBar()
        mainWindow.show()
        mainWindow.ShowProgress()
        myApp.exec_()
....

After making the changes, execute the program and you will see an output window as shown in the following screenshot:

Status bar
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.216