QMessageBox - 메세지창, 경고창, 팝업창 > Pyside6 - GUI for Python

본문 바로가기
사이트 내 전체검색

회원로그인

올서베이넷
무료 온라인 설문 사이트
OVITII
웹 프레젠테이션 도구

Pyside6 - GUI for Python

QMessageBox - 메세지창, 경고창, 팝업창

페이지정보

글쓴이 관리자 조회 39,997 조회 날짜 19-12-04 14:11 / Update:23-12-10 23:03
댓글 0 댓글

SNS 공유

  • 트위터로 보내기
  • 페이스북으로 보내기
  • 구글플러스로 보내기

내용

QMessageBox - 메세지창, 경고창, 팝업창

 

프로그램에서 아래의 그림처럼 메세지창을 보여주는 경우가 있다.

 

1269731307_1575495388.9607.png

 

Python에서 PySide6의 QMessageBox를 이용하여 메세지창을 만들 수 있다.

 

QMessageBox를 사용하기 위해서는 QMessageBox를 import 해야한다.

 

기본 코드는 아래와 같다.

form PySide6.QtWidgets import QMessageBox

 

# QMessageBox를 msgBox로 저장하여, 필요한 내용들을 setting 한다.

 

msgBox = QMessageBox()

msgBox.setWindowTitle("Alert Window") # 메세지창의 상단 제목

msgBox.setWindowIcon(QtGui.QPixmap("info.png")) # 메세지창의 상단 icon 설정

msgBox.setIcon(QMessageBox.Information) # 메세지창 내부에 표시될 아이콘

msgBox.setText("This is title") # 메세지 제목

msgBox.setInformativeText("This is content"# 메세지 내용

msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) # 메세지창의 버튼

msgBox.setDefaultButton(QMessageBox.Yes) # 포커스가 지정된 기본 버튼

 

msgBox.exec() # 클릭한 버튼 결과 리턴

 

setIcon에 넣을 수 있는 icon 종류

1269731307_1575495852.7565.png Question

QMessageBox.Question

1269731307_1575495856.9325.png Information QMessageBox.Information
1269731307_1575495860.9008.png Warning QMessageBox.Warning
1269731307_1575495863.0422.png Critical QMessageBox.Critical
  NoIcon QMessageBox.NoIcon

setIcon의 default는 QMessageBox.NoIcon 이다.

 

 

setStandardButtons에 넣을 수 있는 버튼 종류

Constant

Description

QMessageBox.Ok

An “OK” button defined with the AcceptRole .

QMessageBox.Open

An “Open” button defined with the AcceptRole .

QMessageBox.Save

A “Save” button defined with the AcceptRole .

QMessageBox.Cancel

A “Cancel” button defined with the RejectRole .

QMessageBox.Close

A “Close” button defined with the RejectRole .

QMessageBox.Discard

A “Discard” or “Don’t Save” button, depending on the platform, defined with the DestructiveRole .

QMessageBox.Apply

An “Apply” button defined with the ApplyRole .

QMessageBox.Reset

A “Reset” button defined with the ResetRole .

QMessageBox.RestoreDefaults

A “Restore Defaults” button defined with the ResetRole .

QMessageBox.Help

A “Help” button defined with the HelpRole .

QMessageBox.SaveAll

A “Save All” button defined with the AcceptRole .

QMessageBox.Yes

A “Yes” button defined with the YesRole .

QMessageBox.YesToAll

A “Yes to All” button defined with the YesRole .

QMessageBox.No

A “No” button defined with the NoRole .

QMessageBox.NoToAll

A “No to All” button defined with the NoRole .

QMessageBox.Abort

An “Abort” button defined with the RejectRole .

QMessageBox.Retry

A “Retry” button defined with the AcceptRole .

QMessageBox.Ignore

An “Ignore” button defined with the AcceptRole .

QMessageBox.NoButton

An invalid button.

 

출처 : https://doc.qt.io/qtforpython/PySide6/QtWidgets/QMessageBox.html#

 

 

다만 QMessageBox는 QtWidgets이 있어야 동작한다. 따라서 GUI 프로그램을 만들고, 필요에 따라 QMessageBox를 호출하여 사용하도록 한다.

 

아래와 같이 간단한 버튼과 결과를 볼수 있는  ui 파일을 만들어 messagebox.ui로 저장한다. 

1269731307_1575493493.1262.png

MessageBox objectName =BTN_messagebox

LineEdit objectName = LE_result

 

 

 

전체 코드

import sys

import os

from PySide6 import QtUiTools, QtGui

from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox

 

class MainView(QMainWindow):

 

def __init__(self):

        super().__init__() self.setupUI()

 

def setupUI(self):

        global UI_set

        UI_set = QtUiTools.QUiLoader().load(resource_path("messagebox.ui"))

        UI_set.BTN_messagebox.clicked.connect(buttonclick)

        self.setCentralWidget(UI_set) self.setWindowTitle("GUI Program Test")

        self.setWindowIcon(QtGui.QPixmap(resource_path("./images/jbmpa.png")))

        self.resize(730, 420)

        self.show()

 

def buttonclick():

        # 버튼 클릭시 리턴된 결과값을 받아서 필요에 따라 분기하여 사용한다.

        result = messageBox()

 

        if result == QMessageBox.Yes:

                message = "Yes"

        elif result == QMessageBox.No:

                message = "No"

        elif result == QMessageBox.Cancel:

                message = "Cancel"

 

        UI_set.LE_result.setText(message)

 

def messageBox():

        msgBox = QMessageBox()

        msgBox.setWindowTitle("Alert Window")

        msgBox.setWindowIcon(QtGui.QPixmap("info.png"))

        msgBox.setIcon(QMessageBox.Information)

        msgBox.setText("This is title")

        msgBox.setInformativeText("This is content")

        msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)

        msgBox.setDefaultButton(QMessageBox.Yes)

 

        # 클릭한 버튼의 결과를 int로 반환한다.

        return msgBox.exec()

 

# 파일 경로

# pyinstaller로 원파일로 압축할때 경로 필요함

def resource_path(relative_path):

        if hasattr(sys, '_MEIPASS'):

                return os.path.join(sys._MEIPASS, relative_path)

        return os.path.join(os.path.abspath("."), relative_path)

 

if __name__ == '__main__':

        app = QApplication(sys.argv)

        main = MainView()

        # main.show()

        sys.exit(app.exec())

 

 

 

결과

 

실행

1269731307_1575494002.9279.png

 

Messagebox 버튼을 눌러 QMessageBox가 나타난 결과

1269731307_1575495388.9607.png

 

메세지창의 icon은 그렇게 이쁘지 않다. 따라서 setIcon을 NoIcon으로 변경한 경우는 아래와 같다.

msgBox.setIcon(QMessageBox.Information) ==> msgBox.setIcon(QMessageBox.NoIcon)

1269731307_1575495415.2367.png

 

"Yes" 버튼을 누른 결과

1269731307_1575494017.9856.png

 

 

 

MessageBox window title 에 사용된 icon 파일

 

      info.png               warning.png

1269731307_1575494607.3146.png          1269731307_1575494613.825.png

 

 

출처 : 

QMessageBox - https://doc.qt.io/qtforpython/PySide6/QtWidgets/QMessageBox.html

댓글목록 sfs

총 12 건 , 1 페이지
게시물 검색
Pyside6 - GUI for Python 목록
번호 제목 글쓴이 조회 날짜
1 관리자 37382 05-02
2 관리자 31845 05-02
3 관리자 35013 05-03
4 관리자 25270 05-28
5 관리자 29615 05-28
6 관리자 48082 06-11
7 관리자 48583 06-13
열람중 관리자 39998 12-04
9 관리자 38397 12-04
10 관리자 30969 12-13
11 관리자 21424 12-13
12 관리자 23378 12-13
GNUBOARD_M
Copyright © JBMPA.com All rights reserved.