Python Win32api SetConsoletrlHandler在不应运行时运行

Python Win32api SetConsoletrlHandler在不应运行时运行,python,winapi,Python,Winapi,我试图在Python脚本中从win32api实现SetConsoletrlHandler,以便在单击Windows cmd上的“X”按钮关闭脚本时执行一个函数 我的问题是print_test()在登录后运行,导致open_latest_thread()不运行,而不是仅在意外退出时运行print_test()。我做错什么了吗 def print_test(): print("test") login() win32api.SetConsoleCtrlHandler(print_test(

我试图在Python脚本中从win32api实现SetConsoletrlHandler,以便在单击Windows cmd上的“X”按钮关闭脚本时执行一个函数

我的问题是print_test()在登录后运行,导致open_latest_thread()不运行,而不是仅在意外退出时运行print_test()。我做错什么了吗

def print_test():
    print("test")

login()
win32api.SetConsoleCtrlHandler(print_test(), True)
while True:
    try:
        open_latest_thread()
    except Exception as e:
        print(e)
        continue
这个问题是没有正确定义。它有一个类型输入参数和
BOOL
type返回值。像这样:

def print_test(ctrlType):
    print("test")
    return false
win32api.SetConsoleCtrlHandler(print_test, True)
调用
setConsoletrlHandler
时,使用
print\u test
而不是
print\u test()
作为参数。像这样:

def print_test(ctrlType):
    print("test")
    return false
win32api.SetConsoleCtrlHandler(print_test, True)

有关
DWORD
的信息,您可以参考
ctypes.wintypes.DWORD

我的答案对您有用吗?很抱歉回复太晚。这是我的代码,它似乎没有在退出时关闭selenium浏览器@Foong关于函数执行顺序的初始问题解决了吗?如果是,则执行此行
browser.close()
。但是,为什么
browser.close()
不关闭浏览器是另一个不同的主题。