QMessageBox - 메세지창, 경고창, 팝업창
페이지정보
내용
QMessageBox - 메세지창, 경고창, 팝업창
프로그램에서 아래의 그림처럼 메세지창을 보여주는 경우가 있다.

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 종류
| Question | 
			 QMessageBox.Question  | 
		|
| Information | QMessageBox.Information | |
| Warning | QMessageBox.Warning | |
| Critical | QMessageBox.Critical | |
| NoIcon | QMessageBox.NoIcon | 
setIcon의 default는 QMessageBox.NoIcon 이다.
setStandardButtons에 넣을 수 있는 버튼 종류
| 
			 Constant  | 
			
			 Description  | 
		
|---|---|
| 
			 QMessageBox.Ok  | 
			
			 An “OK” button defined with the   | 
		
| 
			 QMessageBox.Open  | 
			
			 An “Open” button defined with the   | 
		
| 
			 QMessageBox.Save  | 
			
			 A “Save” button defined with the   | 
		
| 
			 QMessageBox.Cancel  | 
			
			 A “Cancel” button defined with the   | 
		
| 
			 QMessageBox.Close  | 
			
			 A “Close” button defined with the   | 
		
| 
			 QMessageBox.Discard  | 
			
			 A “Discard” or “Don’t Save” button, depending on the platform, defined with the   | 
		
| 
			 QMessageBox.Apply  | 
			
			 An “Apply” button defined with the   | 
		
| 
			 QMessageBox.Reset  | 
			
			 A “Reset” button defined with the   | 
		
| 
			 QMessageBox.RestoreDefaults  | 
			
			 A “Restore Defaults” button defined with the   | 
		
| 
			 QMessageBox.Help  | 
			
			 A “Help” button defined with the   | 
		
| 
			 QMessageBox.SaveAll  | 
			
			 A “Save All” button defined with the   | 
		
| 
			 QMessageBox.Yes  | 
			
			 A “Yes” button defined with the   | 
		
| 
			 QMessageBox.YesToAll  | 
			
			 A “Yes to All” button defined with the   | 
		
| 
			 QMessageBox.No  | 
			
			 A “No” button defined with the   | 
		
| 
			 QMessageBox.NoToAll  | 
			
			 A “No to All” button defined with the   | 
		
| 
			 QMessageBox.Abort  | 
			
			 An “Abort” button defined with the   | 
		
| 
			 QMessageBox.Retry  | 
			
			 A “Retry” button defined with the   | 
		
| 
			 QMessageBox.Ignore  | 
			
			 An “Ignore” button defined with the   | 
		
| 
			 QMessageBox.NoButton  | 
			
			 An invalid button.  | 
		
출처 : https://doc.qt.io/qtforpython/PySide6/QtWidgets/QMessageBox.html#
다만 QMessageBox는 QtWidgets이 있어야 동작한다. 따라서 GUI 프로그램을 만들고, 필요에 따라 QMessageBox를 호출하여 사용하도록 한다.
아래와 같이 간단한 버튼과 결과를 볼수 있는 ui 파일을 만들어 messagebox.ui로 저장한다.

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())  | 
		
결과
실행
Messagebox 버튼을 눌러 QMessageBox가 나타난 결과

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

"Yes" 버튼을 누른 결과
MessageBox window title 에 사용된 icon 파일
info.png warning.png
          
출처 :
QMessageBox - https://doc.qt.io/qtforpython/PySide6/QtWidgets/QMessageBox.html
