2012-07-31 79 views
1

我试图编写脚本,双击CTRL后执行一些东西。它在第一次双击之后运行良好,但是然后我得到那个错误。另外,如果我在定时器执行函数triger后一次按下CTRL,我会得到相同的错误。threading.Timer类型错误:需要一个整数

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 

回答

3

Acording到documentation有必须 “返回True” 在OnKeyboardEvent函数的末尾。这是它的样子。

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 
相关问题