我正在编写启动脚本,启动并登录我每天使用的所有应用程序。为此,我使用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 block(没用),我已经切换了应用程序启动的顺序(num7之前的num1,反之亦然),但没有用(第一次运行,第二次崩溃)。
我甚至试图将它们分成两个文件并从第三个文件中运行它#main;&#34;文件。我知道它效率不高,但想知道它是否有所不同......你猜对了:它没有。它仍然在PermissionError上崩溃。
答案 0 :(得分:0)
在0.9.34中,这已得到修复(至少现在已经被禁止,因为点击似乎仍然有用)。因此解决方案是升级PyAutoGUI。