Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time.sleep requires integers?

I'm writing a macro that will click certain spots on the screen when I press a key.

The first time I press a key, everything runs fine.
However, any other key press results in the error:

    time.sleep(0.1)
TypeError: an integer is required

Here is the code:

import win32api
import win32con
import time
import pythoncom
import pyHook
import os

def Click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

def DeleteRun(event):
    Click(1250, 741)
    time.sleep(0.1)
    Click(649,261)
    time.sleep(0.1)
    Click(651, 348)
    time.sleep(0.1)
    Click(800, 442)
    time.sleep(0.1)
    Click(865, 612)


Click(20,20)
KeyGrabber = pyHook.HookManager()
KeyGrabber.KeyDown = DeleteRun
KeyGrabber.HookKeyboard()
pythoncom.PumpMessages()

It seems the first time the DeleteRun function is run by pyHook, time.sleep() accepts floats.
On any following function calls, it seems it only accepts integers.

What is causing this?
I can't wait 5 seconds for the mouse arrangement! It's supposed to save time!

Specs:

  • python 2.7.2
  • Windows 7 (32)
like image 880
Anti Earth Avatar asked Mar 18 '12 21:03

Anti Earth


People also ask

What does time sleep () do in Python?

Python time sleep function is used to add delay in the execution of a program. We can use python sleep function to halt the execution of the program for given time in seconds. Notice that python time sleep function actually stops the execution of current thread only, not the whole program.

Does time sleep accept float?

The sleep() function suspends the execution of a program for a given number of seconds. It also takes in floating point numbers as seconds to stop the execution of a program. sleep() is defined in the time module.

Is python time sleep accurate?

The accuracy of the time. sleep function depends on your underlying OS's sleep accuracy. For non-realtime OS's like a stock Windows the smallest interval you can sleep for is about 10-13ms. I have seen accurate sleeps within several milliseconds of that time when above the minimum 10-13ms.

How do you wait 0.5 seconds in Python?

If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.


1 Answers

Okay, how about this? Add a return True to DeleteRun:

def DeleteRun(event):
    Click(1250, 741)
    time.sleep(0.1)
    [...]
    return True

I should probably confess that this was little more than google-fu: read the answer to this question.

like image 74
DSM Avatar answered Oct 04 '22 00:10

DSM