[Pyqt5] QMessageBox 이용하여 알림창 띄우기 (How to use QMessageBox?) [8]

2021. 6. 28. 08:39GUI Programming/PyQT5 (GUI Programming)

반응형

[ Pyqt5 ]

이번 포스팅은 QMessageBox를 이용하여 알림창을 띄우는 방법에 대해 포스팅 하겠습니다.

 

QMessageBox는 정보 안내 전달이나, 오류가 났을때 많이 사용되곤 합니다.


우선, 메인 윈도우를 구성해줍니다

알림별로 버튼을 5개를 만들어줍니다.

그리고 알림들의 차이를 설명해주도록 하겠습니다

class Ui_MainWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setFixedSize(620, 200)

        self.centralwidget = QtWidgets.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")

         # About_button
        self.about_button = QtWidgets.QPushButton(self.centralwidget)
        self.about_button.setGeometry(QtCore.QRect(20, 90, 100, 30))
        self.about_button.setObjectName("about_button")
        self.about_button.setText("About")
        self.about_button.clicked.connect(self.About_event)

        # Information_button
        self.Information_button = QtWidgets.QPushButton(self.centralwidget)
        self.Information_button.setGeometry(QtCore.QRect(140, 90, 100, 30))
        self.Information_button.setObjectName("Information_button")
        self.Information_button.setText("Information")
        self.Information_button.clicked.connect(self.Information_event)

        # Warning_button
        self.warning_button = QtWidgets.QPushButton(self.centralwidget)
        self.warning_button.setGeometry(QtCore.QRect(260, 90, 100, 30))
        self.warning_button.setObjectName("warning_button")
        self.warning_button.setText("Warning")
        self.warning_button.clicked.connect(self.Warning_event)

        # Question_button
        self.question_button = QtWidgets.QPushButton(self.centralwidget)
        self.question_button.setGeometry(QtCore.QRect(380, 90, 100, 30))
        self.question_button.setObjectName("question_button")
        self.question_button.setText("Question")
        self.question_button.clicked.connect(self.Question_event)

        # Critical_button
        self.critical_button = QtWidgets.QPushButton(self.centralwidget)
        self.critical_button.setGeometry(QtCore.QRect(500, 90, 100, 30))
        self.critical_button.setObjectName("critical_button")
        self.critical_button.setText("Critical")
        self.critical_button.clicked.connect(self.Critical_event)

        self.show()

QMessageBox.about()

about Attribute는 값을 리턴 받을 수 없습니다.

추가로 메세지 전달을 할 때, 프로그램 아이콘으로 표시 해주는것이 특징입니다.

 

QMessageBox.about(self, title, text)

 

[메세지 전달]

# About 버튼 클릭 이벤트
def About_event(self) :
    QMessageBox.about(self,'About Title','About Message')

 

[결과]

(프로그램 아이콘이 없어서 안뜹니다.)


QMessageBox.information()

information Attribute는 사용자에게 정보를 줄 때 사용하고, 값을 리턴 받을 수 있습니다.

추가로 사용에 따라 일방적인 메세지 전달 또한 가능하며,

메세지를 전달할때 information 아이콘을 표시 해줍니다.

 

QMessageBox.information(self, title, text)

 

[메세지 전달]

# Information 버튼 클릭 이벤트
def Information_event(self) :
    QMessageBox.information(self,'Information Title','Information Message')

 

[결과]


QMessageBox.information(self, title, text, 리턴받을 버튼 생성, 기본 선택 버튼)

 

[메세지 전달 및 값 받기]

# Information 버튼  클릭 이벤트
def Information_event(self):
    buttonReply = QMessageBox.information(
        self, 'Information Title', "Information Message", 
        QMessageBox.Yes | QMessageBox.Save | QMessageBox.Cancel | QMessageBox.Reset | QMessageBox.No, 
        QMessageBox.No
        )

    if buttonReply == QMessageBox.Yes:
        print('Yes clicked.')
    elif buttonReply == QMessageBox.Save:
        print('Save clicked.')
    elif buttonReply == QMessageBox.Cancel:
        print('Cancel clicked.')
    elif buttonReply == QMessageBox.Close:
        print('Close clicked.')
    elif buttonReply == QMessageBox.Reset:
        print('Reply clicked.')
    else:
        print('No clicked.')

 

[결과]


QMessageBox.warning()

warning Attribute는 사용자에게 경고를 보낼때 사용하고, 값을 리턴 받을 수 있습니다.

추가로 사용에 따라 일방적인 메세지 전달 또한 가능하며,

메세지를 전달할때 warning 아이콘을 표시 해줍니다.

 

QMessageBox.warning(self, title, text)

 

[메세지 전달]

# Warning 버튼 클릭 이벤트
def Warning_event(self) :
    QMessageBox.warning(self,'Warning Title','Warning Message')

 

[결과]


QMessageBox.warning(self, title, text, 리턴받을 버튼 생성, 기본 선택 버튼)

 

[메세지 전달 및 값 받기]

# Warning 버튼  클릭 이벤트
def Warning_event(self):
    buttonReply = QMessageBox.warning(
        self, 'Warning Title', "Warning Message", 
        QMessageBox.Yes | QMessageBox.Save | QMessageBox.Cancel | QMessageBox.Reset | QMessageBox.No, 
        QMessageBox.No
        )

    if buttonReply == QMessageBox.Yes:
        print('Yes clicked.')
    elif buttonReply == QMessageBox.Save:
        print('Save clicked.')
    elif buttonReply == QMessageBox.Cancel:
        print('Cancel clicked.')
    elif buttonReply == QMessageBox.Close:
        print('Close clicked.')
    elif buttonReply == QMessageBox.Reset:
        print('Reply clicked.')
    else:
        print('No clicked.')

 

[결과]


QMessageBox.question()

question Attribute는 사용자에게 질문을 할때 사용하고, 값을 리턴 받을 수 있습니다.

해당 Attribute는 일방적인 메세지 전달이 불가능하며, Yes 또는 No로 값이 반환이 됩니다.

메세지를 전달할때 question 아이콘을 표시 해줍니다.

 

QMessageBox.question(self, title, text)

 

[메세지 전달]

# Question 버튼 클릭 이벤트
def Question_event(self) :
    QMessageBox.question(self,'Question Title','Question Message')

 

[결과]


QMessageBox.question(self, title, text, 리턴받을 버튼 생성, 기본 선택 버튼)

 

[메세지 전달 및 값 받기]

# Question 버튼 클릭 이벤트
def Question_event(self):
    buttonReply = QMessageBox.question(
        self, 'Question Title', "Question Message", 
        QMessageBox.Yes | QMessageBox.Save | QMessageBox.Cancel | QMessageBox.Reset | QMessageBox.No, 
        QMessageBox.No
        )

    if buttonReply == QMessageBox.Yes:
        print('Yes clicked.')
    elif buttonReply == QMessageBox.Save:
        print('Save clicked.')
    elif buttonReply == QMessageBox.Cancel:
        print('Cancel clicked.')
    elif buttonReply == QMessageBox.Close:
        print('Close clicked.')
    elif buttonReply == QMessageBox.Reset:
        print('Reply clicked.')
    else:
        print('No clicked.')

 

[결과]


QMessageBox.critical()

critical Attribute는 프로그램이 에러가 났을때 사용자한테 알릴 때 사용하고, 값을 리턴 받을 수 있습니다.

추가로 사용에 따라 일방적인 메세지 전달 또한 가능하며,

메세지를 전달할때 critical 아이콘을 표시 해줍니다.

 

QMessageBox.critical(self, title, text)

 

[메세지 전달]

# Critical 버튼 클릭 이벤트
def Critical_event(self) :
    QMessageBox.critical(self,'Critical Title','Critical Message')

 

[결과]


QMessageBox.warning(self, title, text, 리턴받을 버튼 생성, 기본 선택 버튼)

 

[메세지 전달 및 값 받기]

# Critical 버튼 클릭 이벤트
def Critical_event(self):
    buttonReply = QMessageBox.critical(
        self, 'Critical Title', "Critical Message", 
        QMessageBox.Yes | QMessageBox.Save | QMessageBox.Cancel | QMessageBox.Reset | QMessageBox.No, 
        QMessageBox.No
        )

    if buttonReply == QMessageBox.Yes:
        print('Yes clicked.')
    elif buttonReply == QMessageBox.Save:
        print('Save clicked.')
    elif buttonReply == QMessageBox.Cancel:
        print('Cancel clicked.')
    elif buttonReply == QMessageBox.Close:
        print('Close clicked.')
    elif buttonReply == QMessageBox.Reset:
        print('Reply clicked.')
    else:
        print('No clicked.')

 

[결과]


[ 완성된 코드 ]

github.com/Mr-DooSun/pyqt5-gui/tree/master/ex6_message_box

 

Mr-DooSun/pyqt5-gui

Contribute to Mr-DooSun/pyqt5-gui development by creating an account on GitHub.

github.com

 

반응형