如何从弹出按钮获取用户输入(ctypes)

问题描述 投票:0回答:1

所以我使用 ctypes 制作了一个消息框来关闭我的程序:

def kill():
ctypes.windll.user32.MessageBoxW(0, "Thanks for using Chatbot", "Chatbot", 1)
sys.exit()

但是我不确定当用户单击“确定”或“取消”时如何让用户输入,我想取消关闭程序。

python ctypes
1个回答
2
投票

捕获返回值。定义

.argtypes
.restype
也是很好的做法。

import ctypes
import ctypes.wintypes as w

# From the documentation at
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw
MB_OKCANCEL = 1
IDCANCEL = 2
IDOK = 1

user32 = ctypes.WinDLL('user32')
MessageBox = user32.MessageBoxW
MessageBox.argtypes = w.HWND, w.LPCWSTR, w.LPCWSTR, w.UINT
MessageBox.restype = ctypes.c_int

ret = MessageBox(None, 'message', 'title', MB_OKCANCEL)
if ret == IDOK:
    print('OK')
elif ret == IDCANCEL:
    print('CANCEL')
else:
    print(f'{ret=}')
© www.soinside.com 2019 - 2024. All rights reserved.