threading.Timer TypeError:需要一个整数

时间:2012-07-31 17:42:15

标签: python timer typeerror

我正在尝试编写双击CTRL后执行某些内容的脚本。首次双击后效果很好,但后来我得到了这个错误。此外,如果我在定时器执行函数triger之后一次按CTRL,我将得到相同的错误。

import pythoncom, pyHook, threading

press = False

def triger():
    global press
    press=False

def something():
    print 'hello'

def OnKeyboardEvent(event):
    global press
    if event.Key=='Lcontrol':
        if press:
            something()
            press = False
        else:
            press=True
            threading.Timer(1,triger).start()


hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()

错误:

hello

Warning (from warnings module):
  File "C:\Python27\lib\threading.py", line 828
    return _active[_get_ident()]
RuntimeWarning: tp_compare didn't return -1 or -2 for exception
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in KeyboardSwitch
    return func(event)
  File "C:\Users\123\Desktop\code\hooks.py", line 20, in OnKeyboardEvent
    threading.Timer(1,triger).start()
  File "C:\Python27\lib\threading.py", line 731, in Timer
    return _Timer(*args, **kwargs)
  File "C:\Python27\lib\threading.py", line 742, in __init__
    Thread.__init__(self)
  File "C:\Python27\lib\threading.py", line 446, in __init__
    self.__daemonic = self._set_daemon()
  File "C:\Python27\lib\threading.py", line 470, in _set_daemon
    return current_thread().daemon
  File "C:\Python27\lib\threading.py", line 828, in currentThread
    return _active[_get_ident()]
TypeError: an integer is required

1 个答案:

答案 0 :(得分:3)

根据documentation,必须在KeyboardEvent函数结束时“返回True”。这是它的样子。

def OnKeyboardEvent(event):
    global press
    if event.Key=='Lcontrol':
        if press:
            something()
            press = False
        else:
            press=True
            threading.Timer(1,triger).start()
            print 'waiting'
    return True