在 Python 中使用击键结束循环调用函数

Call function with a keystroke ending loop in Python

目前我正在尝试调用一个具有不同功能的函数,该函数循环初始函数直到我自己击键。这是我正在尝试的代码 运行:

def input_thread(a_list):
    raw_input()
    a_list.append(True)

def loop_display(function):
    a_list = []
    thread.start_new_thread(input_thread, (a_list,))
    while not a_list:

        function
        #print "next"

        time.sleep(2)

loop_display(function)

问题是它只运行被调用的函数一次。

当我在循环中打印一些东西时,它循环打印调用,但传入的函数只循环一次。

理想情况下,我想将此循环放入另一个文件中,并在需要时调用它。但如果我不能通过它调用另一个函数,那将不起作用,对吗?

我从 this answer.

中提取了循环代码

注意:如果重要,结束该功能的击键工作正常。

我想你只需要在函数后面加上括号。

`

def function():
    print("hello")

import thread
import time
def input_thread(a_list):
    raw_input()
    a_list.append(True)

def loop_display(function):
    a_list = []
    thread.start_new_thread(input_thread, (a_list,))
    while not a_list:

        function()
        #print "next"

        time.sleep(1)

loop_display(function)

`