Python, Press Any Key To Exit

后端 未结 9 1294
渐次进展
渐次进展 2020-12-31 06:16

So, as the title says, I want a proper code to close my python script. So far, I\'ve used input(\'Press Any Key To Exit\'), but what that does, is generate a er

9条回答
  •  情书的邮戳
    2020-12-31 07:09

    A little late to the game, but I wrote a library a couple years ago to do exactly this. It exposes both a pause() function with a customizable message and the more general, cross-platform getch() function inspired by this answer.

    Install with pip install py-getch, and use it like this:

    from getch import pause
    pause()
    

    This prints 'Press any key to continue . . .' by default. Provide a custom message with:

    pause('Press Any Key To Exit.')
    

    For convenience, it also comes with a variant that calls sys.exit(status) in a single step:

    pause_exit(0, 'Press Any Key To Exit.')
    

    Check it out.

提交回复
热议问题