我发现pyautogui和tkinter之间存在冲突。当我运行没有tkinter的程序时,pyautogui可以控制键盘和键盘。鼠标如预期。但是,如果我首先通过tkinter获得输入(例如用户选择的目录),我会收到此错误:
File "C:\Users\Test_Lab\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyautogui\__init__.py", line 362, in click
platformModule._click(x, y, 'left')
File "C:\Users\Test_Lab\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\Test_Lab\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyautogui\_pyautogui_win.py", line 480, in _sendMouseEvent
raise ctypes.WinError()
FileNotFoundError: [WinError 2] The system cannot find the file specified.
我可以通过先打印出来验证tkinter是否获得了一个有效的目录。版本:Python = 3.5.1,tkinter.TkVersion
= 8.6,pyautogui.__version__
='0.9.33',在Windows 7 Pro上运行。
以下是重现我的问题的最小示例:
import pyautogui
import tkinter
root=tkinter.Tk()
dirname = tkinter.filedialog.askdirectory(parent=root, initialdir="C:\\",
title='Please select a directory')
dname=dirname.replace('/',os.sep)
print(dname)
root.quit()
root.destroy()
pyautogui.moveTo(35, 5)
pyautogui.click(pause=.1)