所以我使用 ctypes 制作了一个消息框来关闭我的程序:
def kill():
ctypes.windll.user32.MessageBoxW(0, "Thanks for using Chatbot", "Chatbot", 1)
sys.exit()
但是我不确定当用户单击“确定”或“取消”时如何让用户输入,我想取消不关闭程序。
捕获返回值。定义
.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=}')