如何获取ctypes MessageBoxA中按下的按钮的值?

时间:2016-01-22 18:20:50

标签: python conditional ctypes messagebox

需要ctypes库的帮助。

根据this link,我可以使用以下代码创建一个消息框:

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

并配置最后一个参数来定义消息框的类型:

##  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
  

如何使用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

我已经尝试过此代码,但未成功:

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

修改

通过查看提供的链接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  

0 个答案:

没有答案