[파이썬] PyQt 진행 상황 표시 (`QProgressBar`)

In many applications, it is important to provide feedback to the user about the progress of a certain task. One way to achieve this in PyQt is by using the QProgressBar widget. The QProgressBar widget provides a visual representation of the progress of a task, allowing the user to know how much work has been completed.

In this blog post, we will explore how to use the QProgressBar widget in Python with the PyQt library.

Setting up the PyQt Environment

Before we dive into using the QProgressBar widget, let’s make sure we have PyQt installed. Run the following command in your terminal:

pip install pyqt5

Once PyQt is installed, we can proceed with implementing the progress bar in our application.

Creating a Simple Progress Bar

To create a basic QProgressBar widget in PyQt, follow these steps:

  1. Import the necessary modules:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QProgressBar
  1. Create a subclass of QMainWindow:
class ProgressBarWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        # Code for creating the progress bar will go here
        pass
  1. Inside the init_ui method, create an instance of QProgressBar and set its initial value:
def init_ui(self):
    self.progress = QProgressBar(self)
    self.progress.setGeometry(10, 10, 400, 30)
    self.progress.setValue(0)
  1. Build and run the application:
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = ProgressBarWindow()
    window.show()
    sys.exit(app.exec())

When you run the application, you should see a window with a progress bar that is empty. The progress bar will be displayed with a width of 400 pixels and a height of 30 pixels.

Updating the Progress Bar

A progress bar wouldn’t be very useful if it stayed empty all the time. To update the progress bar, we will need to connect it to a signal that indicates when progress has been made.

In this example, let’s assume we have a time-consuming task that we want to indicate progress for. We will use a timer to simulate this task.

  1. Import the necessary modules:
from PyQt5.QtCore import QTimer
  1. Modify the init_ui method to add a timer that updates the progress bar at regular intervals:
def init_ui(self):
    self.progress = QProgressBar(self)
    self.progress.setGeometry(10, 10, 400, 30)
    self.progress.setValue(0)

    self.timer = QTimer()
    self.timer.timeout.connect(self.update_progress)
    self.timer.start(1000)  # Updates every second

def update_progress(self):
    value = self.progress.value()
    if value >= 100:
        self.timer.stop()
    else:
        self.progress.setValue(value + 10)

In this example, we set the timer to update the progress bar every second. Each time the update_progress method is called, we check the current value of the progress bar. If it is already at 100% or higher, we stop the timer. Otherwise, we increment the value of the progress bar by 10.

Now, when you run the application, you should see the progress bar fill up gradually until it reaches its maximum value.

Customizing the Progress Bar

The QProgressBar widget provides various customization options to match the style and design of your application.

Here are a few examples of common customization options:

Feel free to experiment with these customization options to achieve the desired look and feel for your progress bar.

Conclusion

In this blog post, we explored how to use the QProgressBar widget in PyQt to display the progress of a task. We covered the basics of creating a progress bar, updating its value, and customizing its appearance.

The QProgressBar widget is a powerful tool that can enhance the user experience of your PyQt applications by providing real-time feedback on the progress of tasks.

I hope you found this blog post helpful in understanding how to use the QProgressBar widget in PyQt. Happy coding!