PyQt5 statusbar

PyQt5 supports a window status bar. To add a status bar add the line:

self.statusBar().showMessage('Message in statusbar.')

A statusbar can be added to the main window (QMainWindow). It is one of the methods the class itself contains;

PyQt5 statusbar example: The program below adds a statusbar to a PyQt5 window:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from PyQt5.QtGui import QIcon

class App(QMainWindow):

def __init__(self): super().__init__() self.title = 'PyQt5 status bar example - pythonspot.com' self.left = 10 self.top = 10 self.width = 640 self.height = 480 self.initUI()

def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.statusBar().showMessage('Message in statusbar.') self.show()

if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_())

The example creates a window (QMainWindow). We set the screen parameters using:

self.left = 10
self.top = 10
self.width = 640
self.height = 480

Window properties are set in the initUI() method which is called in the constructor. The method:

statusBar().showMessage()

sets the text on the statusbar.

PyQt5 statusbar window example showing status message

Screenshot of the PyQt5 status bar window above.

Add permanent widgets to the status bar

You can also add permanent widgets to the right side of the status bar. This is useful for showing things like an internet connection status or a permanent label. Use the addPermanentWidget() method:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 status bar permanent widget'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        
        # Temporary status message
        self.statusBar().showMessage('Ready')
        
        # Permanent widget on the right side
        self.permanent_label = QLabel('Online', self)
        self.statusBar().addPermanentWidget(self.permanent_label)
        
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

The label stays on the right side even when the temporary message on the left changes or expires.

PyQt5 status bar permanent widget with QLabel on right side

Remove or clear status bar text

To clear the current message from the status bar, use:

self.statusBar().clearMessage()

Download PyQT5 Examples

Back ⚡ Practice with PyChallengeNext