如何通过击键终止 while 循环?

问题描述 投票:0回答:17

我正在读取串行数据并使用 while 循环写入 csv 文件。我希望用户能够在他们觉得收集了足够的数据后终止 while 循环。

while True:
    #do a bunch of serial stuff

    #if the user presses the 'esc' or 'return' key:
        break

我已经使用 opencv 做了类似的事情,但它似乎并没有在这个应用程序中工作(而且我真的不想仅仅为了这个功能而导入 opencv)...

        # Listen for ESC or ENTER key
        c = cv.WaitKey(7) % 0x100
        if c == 27 or c == 10:
            break

所以。如何让用户跳出循环?

另外,我不想使用键盘中断,因为脚本需要在while循环终止后继续运行。

python while-loop break
17个回答
183
投票

最简单的方法是用通常的

Ctrl-C
(SIGINT) 打断它。

try:
    while True:
        do_something()
except KeyboardInterrupt:
    pass

由于

Ctrl-C
导致
KeyboardInterrupt
被提升,只需将其捕获在循环之外并忽略它。


48
投票

有一个不需要非标准模块并且100%可移植的解决方案:

import _thread

def input_thread(a_list):
    raw_input()             # use input() in Python3
    a_list.append(True)
    
def do_stuff():
    a_list = []
    _thread.start_new_thread(input_thread, (a_list,))
    while not a_list:
        stuff()

16
投票

以下代码对我有用。它需要 openCV(导入 cv2)。

代码由一个不断寻找按下的键的无限循环组成。在这种情况下,当按下“q”键时,程序结束。可以按下其他键(在此示例中为“b”或“k”)以执行不同的操作,例如更改变量值或执行函数。

import cv2

while True:
    k = cv2.waitKey(1) & 0xFF
    # press 'q' to exit
    if k == ord('q'):
        break
    elif k == ord('b'):
        # change a variable / do something ...
    elif k == ord('k'):
        # change a variable / do something ...

14
投票

对于 Python 3.7,我复制并更改了 user297171 的非常好的答案,因此它适用于我测试的 Python 3.7 中的所有场景。

import threading as th

keep_going = True
def key_capture_thread():
    global keep_going
    input()
    keep_going = False

def do_stuff():
    th.Thread(target=key_capture_thread, args=(), name='key_capture_thread', daemon=True).start()
    while keep_going:
        print('still going...')

do_stuff()

9
投票
pip install keyboard

import keyboard

while True:
    # do something
    if keyboard.is_pressed("q"):
        print("q pressed, ending loop")
        break

5
投票

这是一个对我有用的解决方案。从这里和其他地方的帖子中得到一些想法。在按下定义的键(abortKey)之前,循环不会结束。循环尽可能快地停止并且不会尝试运行到下一次迭代。

from pynput import keyboard
from threading import Thread
from time import sleep

def on_press(key, abortKey='esc'):    
    try:
        k = key.char  # single-char keys
    except:
        k = key.name  # other keys    

    print('pressed %s' % (k))
    if k == abortKey:
        print('end loop ...')
        return False  # stop listener

def loop_fun():
    while True:
        print('sleeping')
        sleep(5)
        
if __name__ == '__main__':
    abortKey = 't'
    listener = keyboard.Listener(on_press=on_press, abortKey=abortKey)
    listener.start()  # start to listen on a separate thread

    # start thread with loop
    Thread(target=loop_fun, args=(), name='loop_fun', daemon=True).start()

    listener.join() # wait for abortKey

4
投票

pyHook 可能会有所帮助。 http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=PyHook_Tutorial#tocpyHook%5FTutorial4

见键盘挂钩;这是更普遍的——如果你想要特定的键盘交互而不仅仅是使用 KeyboardInterrupt。

另外,一般来说(取决于您的使用)我认为仍然可以使用 Ctrl-C 选项来终止您的脚本是有意义的。

另见上一个问题:Detect in python which keys are pressed


2
投票

总是有

sys.exit()
.

Python核心库中的系统库有一个exit函数,在做原型的时候超级方便。 代码如下:

import sys

while True:
    selection = raw_input("U: Create User\nQ: Quit")
    if selection is "Q" or selection is "q":
        print("Quitting")
        sys.exit()
    if selection is "U" or selection is "u":
        print("User")
        #do_something()

1
投票

我修改了 rayzinnz 的答案以使用特定键结束脚本,在本例中为转义键

import threading as th
import time
import keyboard

keep_going = True
def key_capture_thread():
    global keep_going
    a = keyboard.read_key()
    if a== "esc":
        keep_going = False


def do_stuff():
    th.Thread(target=key_capture_thread, args=(), name='key_capture_thread', daemon=True).start()
    i=0
    while keep_going:
        print('still going...')
        time.sleep(1)
        i=i+1
        print (i)
    print ("Schleife beendet")


do_stuff()

1
投票

这是我用线程和标准库找到的解决方案

循环一直持续到按下一个键
将按下的键作为单个字符串返回

适用于 Python 2.7 和 3

import thread
import sys

def getch():
    import termios
    import sys, tty
    def _getch():
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch
    return _getch()

def input_thread(char):
    char.append(getch())

def do_stuff():
    char = []
    thread.start_new_thread(input_thread, (char,))
    i = 0
    while not char :
        i += 1

    print "i = " + str(i) + " char : " + str(char[0])

do_stuff()

1
投票

从这个线程进入兔子洞,我来到了这个,适用于 Win10 和 Ubuntu 20.04。我想要的不仅仅是杀死脚本,而是使用特定的键,它必须在 MS 和 Linux 上都能工作..

import _thread
import time
import sys
import os

class _Getch:
    """Gets a single character from standard input.  Does not echo to the screen."""
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()

class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch

class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        msvcrt_char = msvcrt.getch()
        return msvcrt_char.decode("utf-8")

def input_thread(key_press_list):
    char = 'x'
    while char != 'q': #dont keep doing this after trying to quit, or 'stty sane' wont work
        time.sleep(0.05)
        getch = _Getch()
        char = getch.impl()
        pprint("getch: "+ str(char))
        key_press_list.append(char)

def quitScript():
    pprint("QUITTING...")
    time.sleep(0.2) #wait for the thread to die
    os.system('stty sane')
    sys.exit()

def pprint(string_to_print): #terminal is in raw mode so we need to append \r\n
    print(string_to_print, end="\r\n")

def main():
    key_press_list = []
    _thread.start_new_thread(input_thread, (key_press_list,))
    while True:
        #do your things here
        pprint("tick")
        time.sleep(0.5)

        if key_press_list == ['q']:
            key_press_list.clear()
            quitScript()

        elif key_press_list == ['j']:
            key_press_list.clear()
            pprint("knock knock..")

        elif key_press_list:
            key_press_list.clear()

main()

0
投票

这可能有帮助 用 -- 安装 pynput pip 安装 pynput

from pynput.keyboard import Key, Listener
def on_release(key):
    if key == Key.esc:
        # Stop listener
        return False

# Collect events until released
while True:
    with Listener(
            on_release=on_release) as listener:
        listener.join()
    break 

0
投票

这是一个简单的 Windows 解决方案,可以安全地结束当前迭代然后退出。我将它与一个用“Esc”键中断循环并退出的反例一起使用。它使用来自 msvcrt 包的 kbhit()getch() 函数。仅出于地役权原因调用时间包(设置事件之间的时间延迟)。

import msvcrt, time

print("Press 'Esc' to stop the loop...")
x = 0
while True:
    x += 1
    time.sleep(0.5)
    print(x)
    
    if msvcrt.kbhit():
        if msvcrt.getch() == b'\x1b':
            print("You have pressed Esc! See you!")
            time.sleep(2)    
            break

kbhit() 如果按键等待被读取,函数返回 True

getch() 函数读取按键并将结果字符作为字节字符串返回。它可以与任何键一起使用

b'\x1b' 是 'Esc' 键的字节串字符。


0
投票

这里是另一个使用

threading.Event
的例子,不需要捕捉
SIGINT
Ctrl+c
)。

正如@Atcold 在已接受答案下方的评论中提到的那样,在循环中按

Ctrl+c
可能会中断长时间运行的操作并使其处于未定义状态。当长时间运行的操作来自您正在调用的库时,这会特别烦人。

在下面的示例中,用户需要按

q
,然后按
Enter
。如果你想立即捕捉击键,你需要像
_Getch()
来自 this answer.

import time
from threading import Thread, Event


def read_input(q_entered_event):
    c = input()
    if c == "q":
        print("User entered q")
        q_entered_event.set()


def do_long_running_stuff():
    q_pressed_event = Event()
    input_thread = Thread(target=read_input,
                          daemon=True,
                          args=(q_pressed_event,))
    input_thread.start()
    while True:
        print("I am working ...")
        time.sleep(1)
        if q_pressed_event.is_set():
            break
    
    print("Process stopped by user.")


if __name__  == "__main__":
    do_long_running_stuff()

0
投票
from time import sleep
from threading import Thread
import threading

stop_flag = 0
    
    def Wait_Char():
        global stop_flag
        v = input("Enter Char")
        if(v == "z"):
            stop_flag = 1
    
    
    def h():
        while(True):
            print("Hello Feto")
            time.sleep(1)
            if(stop_flag == 1):
                break
    
    
    thread1 = Thread(target=Wait_Char)
    thread2 = Thread(target=h)
    thread1.start()
    thread2.start()
    print("threads finished...exiting")

这不是最好的方法,但它可以完成你想要的工作,
运行2个线程一个等待你想要停止循环的键
(Wait_Char 方法)
和一个 for 循环
(H法)
并且都看到一个全局变量 stop_flag 控制停止过程 当我按 z

时停止

0
投票
from pynput import keyboard

def on_press(key):
    if key == keyboard.Key.esc:
        return False

i = 0
with keyboard.Listener(on_press=on_press) as listener:
    # Your infinite loop
    while listener.running:
        print(i)
        i=i+1
print("Done")

有效...


-3
投票
import keyboard

while True:
    print('please say yes')
    if keyboard.is_pressed('y'):
         break
print('i got u :) ')
print('i was trying to write you are a idiot ')
print('  :( ')

输入使用'ENTER'

© www.soinside.com 2019 - 2024. All rights reserved.