Python如何捕捉单击Windows命令提示符“关闭”按钮?

2024-04-23 14:23:18 发布

您现在位置:Python中文网/ 问答频道 /正文

当用户单击运行程序的终端右上角的“X”时,我希望有一个确认,表明他们确实想退出程序。你知道吗

到目前为止,我发现最多的是this,但这只适用于“ctrl-c”,而不是单击“X”。 我还发现

def on_exit(sig, fun=None):
    print("I can't prevent the exit")

win32api.SetConsoleCtrlHandler(on_exit, True)

如果能阻止出口那就太好了。你知道吗


Tags: the用户程序none终端ondefexit