[파이썬] PyQt 메시지 박스 (`QMessageBox`)

In PyQt, the QMessageBox class provides a convenient way to display various types of messages to the user. It can be used to show informational messages, warnings, errors, and more. In this blog post, we will explore how to use QMessageBox to display different types of dialogs in Python.

Installation

Before we dive into the code examples, make sure that you have PyQt installed on your system. You can install PyQt using the following command:

pip install pyqt5

Displaying an Information Dialog

To display an information dialog using QMessageBox, you can use the information method. Here’s an example:

from PyQt5.QtWidgets import QApplication, QMessageBox

app = QApplication([])

# Create a simple information dialog
QMessageBox.information(None, "Information", "This is an information message")

app.exec()

In the above code, we create a QApplication instance and then use the information method of QMessageBox to display the dialog. The arguments to the information method are: the parent widget (None in this case), the title of the dialog, and the message content.

Displaying a Warning Dialog

Similarly, you can display a warning dialog using the warning method. Here’s an example:

from PyQt5.QtWidgets import QApplication, QMessageBox

app = QApplication([])

# Create a simple warning dialog
QMessageBox.warning(None, "Warning", "This is a warning message")

app.exec()

In the above code, we use the warning method of QMessageBox to display a warning dialog. The arguments are the same as the information dialog.

Displaying an Error Dialog

To display an error dialog, you can use the critical method. Here’s an example:

from PyQt5.QtWidgets import QApplication, QMessageBox

app = QApplication([])

# Create a simple error dialog
QMessageBox.critical(None, "Error", "This is an error message")

app.exec()

In the code above, we use the critical method of QMessageBox to show an error dialog.

Displaying a Question Dialog

To display a question dialog with a yes/no answer, you can use the question method. Here’s an example:

from PyQt5.QtWidgets import QApplication, QMessageBox

app = QApplication([])

# Show a question dialog with Yes and No buttons
result = QMessageBox.question(None, "Question", "Do you want to proceed?")

if result == QMessageBox.Yes:
    print("User clicked Yes")
else:
    print("User clicked No")

app.exec()

In the code above, we use the question method of QMessageBox to display a dialog with a question and two buttons (Yes and No). The method returns the user’s choice, which we can then check using an if statement.

Conclusion

In this blog post, we explored how to use QMessageBox in PyQt to display different types of dialogs such as information, warning, error, and question dialogs. You can use these dialogs to show messages and get user input in your PyQt applications.

Remember to import the necessary modules (QApplication and QMessageBox) and create a QApplication instance before using QMessageBox. You can customize the dialogs further by adding buttons, icons, and more based on your specific requirements.

Feel free to experiment with the examples provided and incorporate QMessageBox in your own PyQt projects. Happy coding!