如何设置thread.Timer没有函数?

时间:2017-10-05 21:08:10

标签: python python-2.7 timer

我正在处理我的代码以设置计时器。我想设置Timer而不创建函数。

以下是我使用的内容:

def hello(self):
    print "hello, world"
    self.getControl(MyPlayer.Control_EPG_ID).setVisible(False)

def onAction(self, action):
    if action.getId() == ACTION_MOVE_DOWN:
       t = threading.Timer(5.0, self.hello)
       t.start()

我想让它显示出像这样的东西:

t = threading.Timer(5.0)
t.start()
self.getControl(MyPlayer.Control_EPG_ID).setVisible(False)

当我尝试它时,它会给我一个错误。你知道如何在不创建功能的情况下设置定时器吗?如果是这样的话?

2 个答案:

答案 0 :(得分:1)

你应该能够像hello()这样嵌套onAction()。它将使用传递给外部函数的self参数:

def onAction(self, action):
    def hello():
        print "hello, world"
        self.getControl(MyPlayer.Control_EPG_ID).setVisible(False)

    if action.getId() == ACTION_MOVE_DOWN:
       t = threading.Timer(5.0, hello)
       t.start()

答案 1 :(得分:0)

为什么不喜欢这样:

import time
time.sleep(5)   # delays for 5 seconds