Python:运行pyautogui.click

时间:2016-10-29 11:08:15

标签: python python-3.x pyautogui

这是来自Shell的完整回溯:

 File "C:\Users\Sean\Documents\pyships\mouseclicker.py", line 22, in <module>
    pyautogui.click()
  File "C:\Users\Sean\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyautogui\__init__.py", line 362, in click
    platformModule._click(x, y, 'left')
  File "C:\Users\Sean\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyautogui\_pyautogui_win.py", line 437, in _click
    _sendMouseEvent(MOUSEEVENTF_LEFTCLICK, x, y)
  File "C:\Users\Sean\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyautogui\_pyautogui_win.py", line 480, in _sendMouseEvent
    raise ctypes.WinError()
PermissionError: [WinError 5] Access is denied.

我目前的错误解决方法是在_pyautogui_win.py中注释掉以下几行:

if ctypes.windll.kernel32.GetLastError() != 0:
raise ctypes.WinError()

Python以管理员模式运行,不确定如何正确修复此问题。特别是因为它昨天工作,今天只是抛出错误。最奇怪的部分是它实际执行了点击,然后挂起脚本。

导致它的特定代码行是:

    pyautogui.press('d')
    pyautogui.click()
    pyautogui.keyUp('d')

如果有人知道将关键修饰符附加到点击的更好方法,那也很棒!

1 个答案:

答案 0 :(得分:0)

我现在有3个解决方法:

1您的解决方案可以注释掉_pyautogui_win.py的部分:

2创建一个特别忽略WinError 5的错误陷阱

def ClickFix():
    try:
        pyautogui.click()
    except PermissionError:
        pass

3制作自己的包装

pip install win32api

这将安装win32api,然后为win32api制作一个简单的点击包装使用此功能的先决条件:

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)

感谢PyAutoGui click permissions error代表#2

感谢#{3}}#3