[파이썬] PyQt 상태바 (`QStatusBar`)

In this blog post, we will explore the use of the QStatusBar widget in PyQt, a powerful GUI library for Python. The QStatusBar provides a simple and effective way to display status messages, progress indicators, and other informative content in your PyQt applications.

What is a QStatusBar?

The QStatusBar is a widget that is typically placed at the bottom of a PyQt application’s main window. It provides a dedicated space for displaying status information in a user-friendly manner. It can be used to convey messages, show progress of long-running tasks, or provide any other kind of feedback to the user.

Creating a QStatusBar

To create a QStatusBar in PyQt, we need to import the necessary modules and create an instance of the QStatusBar class.

from PyQt5.QtWidgets import QMainWindow, QApplication, QStatusBar

class MyMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)

In the above code snippet, we first import the required modules and then define a QMainWindow subclass called MyMainWindow. Inside the __init__ method of this class, we create an instance of QStatusBar and set it as the main window’s status bar using the setStatusBar method.

Adding Widgets to the QStatusBar

The QStatusBar widget can also be used to display other widgets, such as labels, buttons, or progress bars. To add widgets to the status bar, we can use the addWidget method.

from PyQt5.QtWidgets import QLabel

self.label = QLabel("Ready")
self.statusBar.addWidget(self.label)

In the above code snippet, we create a QLabel widget with the initial text “Ready” and add it to the status bar using the addWidget method. This label will be displayed in the status bar.

Updating the QStatusBar

We can dynamically update the contents of the QStatusBar by using the showMessage method.

self.statusBar.showMessage("Processing data...")

In the above code snippet, we call the showMessage method of the status bar and pass the desired message as an argument. This will display the message in the status bar.

Conclusion

The QStatusBar widget in PyQt provides an easy way to display status information and messages in your Python GUI applications. By using it effectively, you can enhance the user experience and provide informative feedback. Explore the various features and options offered by the QStatusBar widget to create impressive and user-friendly PyQt applications.

Happy coding with PyQt!