【问题标题】:How to see what the user clicks in ctypes message box如何查看用户在 ctypes 消息框中单击的内容
【发布时间】:2021-08-31 07:39:08
【问题描述】:
我有这个代码,我想知道用户是点击确定还是取消。
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)
如果有人知道如何存储用户输入以通过以下两种方式之一继续代码(“确定”会导致一条代码路径,“取消”会导致另一条代码路径),请告诉我。
【问题讨论】:
标签:
python
ctypes
messagebox
【解决方案1】:
当MessageBoxW windows API 调用成功时,返回值将是described here 的代码之一。
| Return code/value |
Description |
| IDCANCEL 2 |
The Cancel button was selected. |
| IDOK 1 |
The OK button was selected. |
简而言之,如果选择了Cancel 按钮,则返回2,如果选择了OK 按钮,则返回1。因此,您可以将返回值与这些代码进行匹配,以识别用户选择了OK 或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")