Rya*_*uri 3 python ctypes python-2.7 python-3.x
def addnewunit(title, text, style):
ctypes.windll.user32.MessageBoxW(0, text, title, style)
Run Code Online (Sandbox Code Playgroud)
我已经看到很多人展示了这段代码,但是没有人指定如何实际使是/否工作。它们是按钮,它们就在那里,但是如何指定单击或单击时实际发生的情况?
像这样带有适当的 ctypes 包装:
#!/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()
Run Code Online (Sandbox Code Playgroud)
有关utype 参数的各种值,请参阅MessageBox的文档。
| 归档时间: |
|
| 查看次数: |
9522 次 |
| 最近记录: |