1

Need some help with ctypes library.

According to this link I can create a messagebox using the following code:

import ctypes  # An included library with Python install.
ctypes.windll.user32.MessageBoxA(0, "Your text", "Your title", 1)

And configure the last parameter to define the type of messagebox:

##  Styles:
##  0 : OK
##  1 : OK | Cancel
##  2 : Abort | Retry | Ignore
##  3 : Yes | No | Cancel
##  4 : Yes | No
##  5 : Retry | No 
##  6 : Cancel | Try Again | Continue

How can I create a conditional statement using the pressed button inside my ctypes.windll.user32.MessageBoxA ?

def close_program(self, event):
    ctypes.windll.user32.MessageBoxA(0, "Are you sure?", "Alert!", 4)
    if response == "Yes":
        sys.exit(0)
    else:
        pass

I've already tried this code, unsuccessfully:

def close_program(self, event):
    if  ctypes.windll.user32.MessageBoxA(0, "Are you sure?", "Alert!", 4) == "Yes":
        sys.exit(0)
    else:
        pass

EDIT:

Got a solution by looking at the provided link here.

def close_program(self, evt):
        # ctypes messagebox return values
        MB_OK = 0
        MB_OKCANCEL = 1
        MB_YESNOCANCEL = 3
        MB_YESNO = 4
        IDOK = 0
        IDCANCEL = 2
        IDABORT = 3
        IDYES = 6
        IDNO = 7

        resp = ctypes.windll.user32.MessageBoxA(0, "Are you sure?", "Alert!", 4)
        if  resp == IDYES:
            sys.exit(0)
        else:
            pass  
Community
  • 1
  • 1
dot.Py
  • 5,007
  • 5
  • 31
  • 52
  • Thanks mate! Your answer helped me a lot.. :-) – dot.Py Jan 22 '16 at 18:39
  • Instead of adding yet another question, where an answer is now edited into the question, delete yours, it doesn't add anything to SO. – Ulrich Eckhardt Jan 22 '16 at 18:42
  • 1
    Watch out when using `windll`. Someone may have a library that sets `MessageBoxA.errcheck` in a custom way so that it doesn't return what you expect, or a weird `argtypes` that requires a custom class that implements `from_param`. Now your modules contend with each other for the global cache on `ctypes.windll`. It's best to not use this loader at all; it was a horrible idea from the start. Use `user32 = ctypes.WinDLL('user32', use_last_error=True)`. – Eryk Sun Jan 22 '16 at 18:47

0 Answers0