4
def addnewunit(title, text, style):
    ctypes.windll.user32.MessageBoxW(0, text, title, style)

Ive seen a lot of people show this code, however nobody has ever specified how to actually make the Yes/No work. Theyre buttons, and they are there, however how does one specify what actually happens when you click either or?

Ryan Mansuri
  • 99
  • 2
  • 3
  • 6

4 Answers4

6

Something like this with proper ctypes wrapping:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import ctypes
from ctypes.wintypes import HWND, LPWSTR, UINT

_user32 = ctypes.WinDLL('user32', use_last_error=True)

_MessageBoxW = _user32.MessageBoxW
_MessageBoxW.restype = UINT  # default return type is c_int, this is not required
_MessageBoxW.argtypes = (HWND, LPWSTR, LPWSTR, UINT)

MB_OK = 0
MB_OKCANCEL = 1
MB_YESNOCANCEL = 3
MB_YESNO = 4

IDOK = 1
IDCANCEL = 2
IDABORT = 3
IDYES = 6
IDNO = 7


def MessageBoxW(hwnd, text, caption, utype):
    result = _MessageBoxW(hwnd, text, caption, utype)
    if not result:
        raise ctypes.WinError(ctypes.get_last_error())
    return result


def main():
    try:
        result = MessageBoxW(None, "text", "caption", MB_YESNOCANCEL)
        if result == IDYES:
            print("user pressed ok")
        elif result == IDNO:
            print("user pressed no")
        elif result == IDCANCEL:
            print("user pressed cancel")
        else:
            print("unknown return code")
    except WindowsError as win_err:
        print("An error occurred:\n{}".format(win_err))

if __name__ == "__main__":
    main()

See the documentation for MessageBox for the various value of the utype argument.

Neitsa
  • 7,693
  • 1
  • 28
  • 45
3

Quoting official docs:

Return value

Type: int

If a message box has a Cancel button, the function returns the IDCANCEL value if either the ESC key is pressed or the Cancel button is selected. If the message box has no Cancel button, pressing ESC has no effect. If the function fails, the return value is zero. To get extended error information, call GetLastError. If the function succeeds, the return value is one of the following menu-item values

You may checked listed values under official docs link.

Sample code would be something like:

def addnewunit(title, text, style):
    ret_val = ctypes.windll.user32.MessageBoxW(0, text, title, style)
    if ret_val == 0:
        raise Exception('Oops')
    elif ret_val == 1:
        print "OK Clicked"
    ...  # additional conditional checks of ret_val may go here
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
1

You set the answer as equal to the command, like so:

import ctypes
answer = ctypes.windll.user32.MessageBoxW(0, "Message", "Title", 4) 

Then you can just use

print(answer)

to see what number is the outcome of the user's choice.

Then use an "If" statement based on what the answer number is to make it do what you want.

Python Pastor
  • 137
  • 10
0

Other answers show that the return value indicates what button is pressed, but if you don't want to look up all the constant values use pywin32, which has already wrapped much of the Windows API for you (Demo in Chinese to show it is calling Unicode API):

#coding:utf8
import win32api
import win32con
result = win32api.MessageBox(None,'你是美国人吗?','问题',win32con.MB_YESNO)
if result == win32con.IDYES:
    print('是')
else:
    print('不是')
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251