如何通过检测按键来打破Python中的这个循环

VaF*_*ncy 1 python python-2.7 raspberry-pi

from subprocess import call
try:
    while True:
        call (["raspivid -n -b 2666666.67 -t 5000 -o test.mp4"],shell=True)
        call (["raspivid -n -b 2666666.67 -t 5000 -o test1.mp4"],shell=True)
except KeyboardInterrupt:
    pass
Run Code Online (Sandbox Code Playgroud)

当我按任何按钮时,我打算让它打破循环.但是我尝试了许多方法来打破它们并且没有一个工作.

Ale*_*ton 7

您希望您的代码更像这样:

from subprocess import call

while True:
    try:
        call(["raspivid -n -b 2666666.67 -t 5000 -o test.mp4"], shell=True)
        call(["raspivid -n -b 2666666.67 -t 5000 -o test1.mp4"], shell=True)
    except KeyboardInterrupt:
        break  # The answer was in the question!
Run Code Online (Sandbox Code Playgroud)

break完全按照你的期望循环.