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

时间:2018-04-15 02:10:00

标签: python python-2.7 function while-loop call

目前我试图用一个不同的函数调用一个函数来循环初始函数,直到我自己的击键。这是我试图运行的代码:

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.

中提取了循环代码

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

1 个答案:

答案 0 :(得分:1)

我认为你只需要在功能之后添加括号。

`

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)

`