Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从弹出窗口(ctypes)上的按钮获取用户输入_Python_Ctypes - Fatal编程技术网

Python 如何从弹出窗口(ctypes)上的按钮获取用户输入

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

因此,我使用ctypes创建了一个messagebox来关闭我的程序:

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)