PyQt - QStatusBar Widget



The status bar is a section located at the bottom of a primary window that serves to provide information about the current state of the window, such as what is being viewed and how it is being viewed. Additionally, it may display information about background tasks, such as printing, scanning, and formatting, or other contextual information, such as selection and keyboard state.

Furthermore, the status bar shows updates on the page as it loads. Most browsers keep the status bar hidden and most of the time it will only appear when there is something to display.

QMainWindow object reserves a horizontal bar at the bottom as the status bar. It is used to display either permanent or contextual status information.

There are three types of status indicators −

  • Temporary − It covered the horizontal status bar at the bottom of the window. This explains tool tip texts or menu entries.
  • Normal − It occupies a portion of the status bar and is hidden from a temporary message. Thus, this is used to display the page and line number in a word processor.
  • Permanent − The third and final type is 'Permanent' and this is never hidden. It is used for important mode indications such as some applications which put a Caps Lock indicator in the status bar.

Status bar of QMainWindow is retrieved by statusBar() function. status_Bar.method_name function activates it.

Note that, we can directly the method self.statusBar() to get the QStatusBar object and call the other methods such as addWidget(), addPermanentWidget() and showMessage().

self.statusBar = QStatusBar()
self.status_Bar.method_name()

Methods of QStatusBar Class

Below are the list of methods with its descriptions in detail −

Sr.No. Methods & Description
1

addWidget()

Adds the given widget object in the status bar

2

addPermanentWidget()

Adds the given widget object in the status bar permanently

3

showMessage()

Displays a temporary message in the status bar for a specified time interval

4

clearMessage()

Removes any temporary message being shown

5

removeWidget()

Removes specified widget from the status bar.

Example

In the following example, we illustrate the task for status bar i.e. present at the bottom of the window.

 
import sys
from PyQt6.QtWidgets import (QApplication, QMainWindow, QLabel, QPushButton)
from PyQt6.QtCore import Qt
class MyWindow(QMainWindow):
   def __init__(self):
      super().__init__()

      self.setWindowTitle("QStatusBar")
      self.setFixedSize(500, 300)

      # Create a status bar
      self.status_bar = self.statusBar()

      # Permanent label (Caps Lock indicator)
      self.caps_lock_label = QLabel("Caps Lock: OFF")
      self.status_bar.addPermanentWidget(self.caps_lock_label)

      # Normal label (Current Page)
      self.page_label = QLabel("Current Page: 1")
      self.status_bar.addWidget(self.page_label)

      # Button to create a temporary messages
      self.show_message_button = QPushButton("Show Temporary Message")
      self.show_message_button.clicked.connect(self.show_temporary_message)
      self.setCentralWidget(self.show_message_button)
   def show_temporary_message(self):
      self.status_bar.showMessage("This message disappears in 4 seconds", 4000)

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MyWindow()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

QStatusBar
Advertisements