PyQt5 - How to add Separator in Status Bar?

PyQt5 - How to add Separator in Status Bar?

In PyQt5, you can add a separator to the status bar of a main window to visually separate different components or messages. The status bar in PyQt5 doesn't have a built-in method for adding a separator directly, but you can achieve this by adding widgets to the status bar and styling them to look like separators.

Here's how you can add a separator to a status bar in PyQt5:

1. Import PyQt5 Modules

First, import the necessary PyQt5 modules:

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QStatusBar from PyQt5.QtCore import Qt import sys 

2. Create the Main Window and Status Bar

Create a main window class and set up the status bar:

class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Status Bar Separator Example") self.setGeometry(100, 100, 600, 400) # Create the status bar self.statusbar = self.statusBar() self.init_statusbar() def init_statusbar(self): # Create status bar widgets self.status_left = QLabel("Left Message") self.status_right = QLabel("Right Message") # Create a separator separator = QLabel(" | ") separator.setStyleSheet("color: gray;") # Add widgets to the status bar self.statusbar.addPermanentWidget(self.status_left) self.statusbar.addPermanentWidget(separator) self.statusbar.addPermanentWidget(self.status_right) 

In the init_statusbar method:

  • Two QLabel widgets are created to hold messages on the left and right side of the separator.
  • A separator label is created to act as a visual separator, styled with a gray color.
  • These widgets are added to the status bar using addPermanentWidget.

3. Set Up the Application

Set up the PyQt application and display the main window:

if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() sys.exit(app.exec_()) 

Complete Example

Here is the complete code to create a PyQt5 window with a status bar that includes a separator:

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel import sys class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Status Bar Separator Example") self.setGeometry(100, 100, 600, 400) self.statusbar = self.statusBar() self.init_statusbar() def init_statusbar(self): self.status_left = QLabel("Left Message") self.status_right = QLabel("Right Message") separator = QLabel(" | ") separator.setStyleSheet("color: gray;") self.statusbar.addPermanentWidget(self.status_left) self.statusbar.addPermanentWidget(separator) self.statusbar.addPermanentWidget(self.status_right) if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() sys.exit(app.exec_()) 

When you run this script, you will see a main window with a status bar at the bottom. The status bar will display two messages separated by a vertical bar (|) acting as a separator. You can customize the messages, the separator's style, and the layout of the status bar as needed for your application.


More Tags

oracleclient common-table-expression vertical-alignment stylish broadcastreceiver android-permissions pygtk sockets graphql-tag coffeescript

More Programming Guides

Other Guides

More Programming Examples