Python 如何从弹出窗口(ctypes)上的按钮获取用户输入
因此,我使用ctypes创建了一个messagebox来关闭我的程序:Python 如何从弹出窗口(ctypes)上的按钮获取用户输入,python,ctypes,Python,Ctypes,因此,我使用ctypes创建了一个messagebox来关闭我的程序: def kill(): ctypes.windll.user32.MessageBoxW(0, "Thanks for using Chatbot", "Chatbot", 1) sys.exit() 但我不确定当用户单击“确定”或“取消”时如何获取用户输入,我希望取消以而不是关闭程序。捕获返回值。定义.argtypes和.restype也是一种很好的做法 import ctypes
def kill():
ctypes.windll.user32.MessageBoxW(0, "Thanks for using Chatbot", "Chatbot", 1)
sys.exit()
但我不确定当用户单击“确定”或“取消”时如何获取用户输入,我希望取消以而不是关闭程序。捕获返回值。定义
.argtypes
和.restype
也是一种很好的做法
import ctypes
from ctypes import wintypes as w
# From the documentation at
# https://docs.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('ret =', ret)