pyautogui crashes whenever it clicks

▼魔方 西西 提交于 2019-12-12 13:35:11

问题


basicly whenever I call pyautogui to click it does it but then crashes the program. here is the program:

import pyautogui
import time
pyautogui.click(650, 200, 10)
print("started")
while 2 == 2:
    x+1
    waittime = random.randrange(35, 40, 1)
    pyautogui.click(600, 680, waittime)
    pyautogui.click(1270, 0, 5)
    if (x % 4) == 0:
        pyautogui.click(600, 550, 4)

when I run it from the command prompt I get this error

Traceback (most recent call last):
  File "C:\Users\dogja\Desktop\crap\region2\scriptybob\test.py", line 3, in <module>
    pyautogui.click(650, 200, 10)
  File "C:\Users\dogja\AppData\Local\Programs\Python\Python35\lib\site-    packages\pyautogui\__init__.py", line 362, in click
    platformModule._click(x, y, 'left')
  File "C:\Users\dogja\AppData\Local\Programs\Python\Python35\lib\site-    packages\pyautogui\_pyautogui_win.py", line 437, in _click
    _sendMouseEvent(MOUSEEVENTF_LEFTCLICK, x, y)
  File "C:\Users\dogja\AppData\Local\Programs\Python\Python35\lib\site-    packages\pyautogui\_pyautogui_win.py", line 480, in _sendMouseEvent
    raise ctypes.WinError()
OSError: [WinError 127] The specified procedure could not be found.

回答1:


Maybe you are not using the click function in right way. See the function definition:

click(x=None, y=None, clicks=1, interval=0.0, button='left', duration=0.0, tween=, pause=None, _pause=True)

Using pyautogui.click(650, 200, 10) you are saying x=650, y=200 and clicks=10. I guess you want to say pyautogui.click(650, 200, interval=10).




回答2:


The problem may have been being caused by the fact that the latest version of pyautogui was intended for python 3.4 when the latest is 3.5. I found that if you're running Windows, you can use win32api. To install this, run the command prompt in admin mode and cd to your python script directory and run this command:

pip install win32api

This will install win32api and its prerequisites.

Then, to make a simple click wrapper for win32api, use this function:

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)

This essentially moves the cursor somewhere, presses the left mouse button down, and releases it very fast. I did not write the click snippet shown above, but I could not find where I found it first. Sorry to whoever wrote that snippet.



来源:https://stackoverflow.com/questions/36824324/pyautogui-crashes-whenever-it-clicks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!