[Python] Windows 메시지 팝업 띄우기 (MessageBox with Windows API)
tkinter나 pyqt 등을 사용하지 않고
Windows API를 이용하여 메시지 팝업을 띄우고 싶다면 아래와 같은 방법을 이용한다.
import win32api
win32api.MessageBox(0, "원하는 메시지를 입력하세요.", "title", 16)
import ctypes
ctypes.windll.user32.MessageBoxW(0, "원하는 메시지를 입력하세요.", "title", 16)
4번째 인수에 어떤 숫자를 넣느냐에 따라 X표시나 ?, ! 등이 뜨며
예/아니오 등의 선택지가 있는 팝업도 띄울 수 있다.
4번째 인수 | 팝업 모양 |
0 | ![]() |
1 | ![]() |
2 | ![]() |
3 | ![]() |
4 | ![]() |
5 | ![]() |
6 | ![]() |
16 | ![]() |
17 | ![]() |
18 | ![]() |
19 | ![]() |
20 | ![]() |
21 | ![]() |
22 | ![]() |
32 | ![]() |
33 | ![]() |
34 | ![]() |
35 | ![]() |
36 | ![]() |
37 | ![]() |
38 | ![]() |
48 | ![]() |
49 | ![]() |
50 | ![]() |
51 | ![]() |
52 | ![]() |
53 | ![]() |
54 | ![]() |
64 | ![]() |
65 | ![]() |
66 | ![]() |
67 | ![]() |
68 | ![]() |
69 | ![]() |
70 | ![]() |
각 인수가 의미하는 바는 다음과 같다.
1) 첫번째 인수 : 윈도우의 핸들값
2) 두번째 인수 : 메시지 팝업에 표시할 내용
3) 세번째 인수 : 팝업의 타이틀
4) 네번째 인수 : 팝업의 종류
참고)
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox
MessageBox function (winuser.h) - Win32 apps
Displays a modal dialog box that contains a system icon, a set of buttons, and a brief application-specific message, such as status or error information. The message box returns an integer value that indicates which button the user clicked.
docs.microsoft.com
* 팝업이 생성될 때 "가장 위에" 뜨면서 + 팝업에 "포커싱"을 하고 싶다면 다음 내용을 추가한다.
import win32api
from win32con import MB_TOPMOST # MB_TOPMOST = 262144
from win32gui import GetForegroundWindow
win32api.MessageBox(GetForegroundWindow(), "원하는 메시지를 입력하세요.", "title", (16 | MB_TOPMOST))
** 덧붙여서 tkinter를 사용한다면 다음과 같이 작성한다.
from tkinter import messagebox as msgbox
# 알림 팝업
msgbox.showinfo("title", "원하는 메시지를 입력하세요.")
# 경고 / 에러 팝업
msgbox.showwarning("title", "원하는 메시지를 입력하세요.")
msgbox.showerror("title", "원하는 메시지를 입력하세요.")
# 질문 팝업
msgbox.askquestion("title", "원하는 메시지를 입력하세요.")
msgbox.askokcancel("title", "원하는 메시지를 입력하세요.")
msgbox.askretrycancel("title", "원하는 메시지를 입력하세요.")
msgbox.askyesno("title", "원하는 메시지를 입력하세요.")
msgbox.askyesnocancel("title", "원하는 메시지를 입력하세요.")
참고) tkinter messagebox