Help us understand the problem. What is going on with this article?

Pythonでキー入力を用いてLoopを抜けるには?

PythonでWhileなどのループを手元のキー入力で抜けたいときにどうすればいいんでしょう?
opencv使ってた身としてはwaitKey(0)的な挙動を期待したいのですが…

考えうる手法群

プログラムを走らせながらなんらかのキー入力をノンブロッキングで取得するには

などが考えられます。
でもコマンド操作でBreakするだけならCtrl+Cをキャッチすれば良さそうです。

Ctrl+Cをキャッチ

Ctrl+CはKeyboardInterruptという例外としてキャッチできるようです。

https://qiita.com/keisuke-nakata/items/e77b123d75d4b52aac77

上の記事によるならば以下のtry構文でCtrl+C後の処理などについてきちんと書けば良いでしょう。

try:
    while True:  # なんらかの重い処理 (for だったり while だったり。。。)
        pass  # ここに、Ctrl-C で止めたい処理を書く
except KeyboardInterrupt:
    # Ctrl-C を捕まえた!
    # print('interrupted!')
    # なにか特別な後片付けが必要ならここに書く
    sys.exit(0)

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away