0

i have this code and i would like to know whether the user clicks ok or cancel.

import ctypes
def Mbox(title, text, style):
    return ctypes.windll.user32.MessageBoxW(0, text, title, style)
Mbox('User Input', 'Click OK to continue or Cancel to stop', 1)

If anyone knows how to store the users input to continue the code in one of two ways ("Ok" leads to one path of code and "Cancel" leads to another path of code) please tell me.

  • Might want to check [\[SO\]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer)](https://stackoverflow.com/questions/58610333/c-function-called-from-python-via-ctypes-returns-incorrect-value/58611011#58611011). – CristiFati Sep 01 '21 at 20:42

1 Answers1

0

When the MessageBoxW windows API call succeeds the return value will be one of the codes as described here.

Return code/value Description
IDCANCEL 2 The Cancel button was selected.
IDOK 1 The OK button was selected.

In short, 2 will be returned if the Cancel button was selected and 1 will be returned if the OK button was selected. So you could match the return value against these codes to identify the user have selected OK or Cancel.

>>> IDOK = 1
>>> IDCANCEL = 2
>>>
>>> code = Mbox('User Input', 'Click OK to continue or Cancel to stop', 1)
>>> if code == IDOK:
...     print("User selected OK...")
... elif code == IDCANCEL:
...     print("User selected Cancel")
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46