我正在编写一个启动脚本,该脚本启动并登录我每天使用的所有应用程序。为此,我使用 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("PASSWORD\n")
# 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:\files\PyCharm\startup\smv2.py", line 10, in start
pyautogui.dragTo(1919, 10, duration=1, button="left")
File "d:\PortableApps\Miniconda3\lib\site-packages\PyAutoGUI-0.9.33-py3.5.egg\pyautogui\__init__.py", line 683, in dragTo
File "d:\PortableApps\Miniconda3\lib\site-packages\PyAutoGUI-0.9.33-py3.5.egg\pyautogui\__init__.py", line 274, in mouseDown
File "d:\PortableApps\Miniconda3\lib\site-packages\PyAutoGUI-0.9.33-py3.5.egg\pyautogui\_pyautogui_win.py", line 393, in _mouseDown
File "d:\PortableApps\Miniconda3\lib\site-packages\PyAutoGUI-0.9.33-py3.5.egg\pyautogui\_pyautogui_win.py", line 480, in _sendMouseEvent
PermissionError: [WinError 5] Access denied.
我正在绞尽脑汁想弄清楚为什么它在第二次运行时崩溃了。我尝试使用 try 和 except 块(没有用),我已经切换了应用程序启动的顺序(num1 在 num7 之前,反之亦然),但没有用(第一次运行,第二次崩溃)。
我什至尝试将它们分成 2 个文件并从第三个“main.py”文件运行它。我知道,它效率不高,但想看看它是否有任何不同......你猜对了:它没有。它仍然在 PermissionError 上崩溃。