PyAutoGui模块在第二次运行时给出PermissionError



我正在编写一个启动脚本,用于启动和登录我每天使用的所有应用程序。为此,我使用PyAutoGui模块,python 3和另一个应用程序来管理我的2台显示器上的窗口布局。

def smv(username, hotkey):
    # starting application
    os.system("smv.exe")
    # it autofocuses on the username field
    pyautogui.typewrite(username)
    # (passwords are the same)
    pyautogui.typewrite("PASSWORDn")
    # move to the window header (where title, minimize, close, etc..)
    pyautogui.moveTo(100, 10, duration=0)
    # drag the window to the far right of the primary monitor (since pyautogui doesn't support multiple monitors)
    pyautogui.dragTo(1910, 20, duration=1, button="left")
    # press the hotkey that belongs to the window layout manager
    pyautogui.hotkey("ctrl", "alt", hotkey)
smv("username", "num7")
smv("username2", "num1")

奇怪的是,第一个smv()运行得很好。应用程序启动,登录,移动窗口和调整位置/大小与热键。问题发生在第二个smv()运行时。应用程序启动,登录,移动到窗口头,但随后崩溃:

Traceback (most recent call last):
File "main.py", line 15, in <module>
   smv("username2", "num1")
File "D:filesPyCharmstartupsmv2.py", line 10, in start
  pyautogui.dragTo(1919, 10, duration=1, button="left")
File "d:PortableAppsMiniconda3libsite-packagesPyAutoGUI-0.9.33-py3.5.eggpyautogui__init__.py", line 683, in dragTo
File "d:PortableAppsMiniconda3libsite-packagesPyAutoGUI-0.9.33-py3.5.eggpyautogui__init__.py", line 274, in mouseDown
File "d:PortableAppsMiniconda3libsite-packagesPyAutoGUI-0.9.33-py3.5.eggpyautogui_pyautogui_win.py", line 393, in _mouseDown
File "d:PortableAppsMiniconda3libsite-packagesPyAutoGUI-0.9.33-py3.5.eggpyautogui_pyautogui_win.py", line 480, in _sendMouseEvent
PermissionError: [WinError 5] Access denied.

我绞尽脑汁想弄明白为什么它在第二次运行时会崩溃。我尝试过使用try和except块(没有使用),我已经切换了应用程序启动的顺序(num1在num7之前,反之亦然),但是没有使用(第一个运行,第二个崩溃)。

我甚至尝试将它们分成两个文件,并从第三个"main.py"文件运行它。我知道这样做效率不高,但我想看看是否有什么不同。你猜对了:它没有。它仍然会在PermissionError时崩溃

在0.9.34中,这个问题已经得到了修复(或者至少,异常现在被抑制了,因为点击似乎仍然可以工作)。所以解决方案是升级PyAutoGUI。

最新更新